CMake版本的细微差异导致find_package(GTest)不能发现GTest的全部库

在Orin上用CMake编译某个程序时,总是报错:

-- Found GTest: /usr/lib/aarch64-linux-gnu/libgtest.a  
-- Configuring done
CMake Error at tests/CMakeLists.txt:8 (add_executable):
  Target "rb_test" links to target "GTest::gtest_main" but the target was
  not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?
Call Stack (most recent call first):
  tests/rb_test/CMakeLists.txt:1 (add_target_test)

/usr/bin/ld: cannot find -lGTest::gtest_main
collect2: error: ld returned 1 exit status
make[2]: *** [tests/rb_test/CMakeFiles/rb_test.dir/build.make:85: tests/rb_test/rb_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:992: tests/rb_test/CMakeFiles/rb_test.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....

GTest实际上有两个库:/usr/lib/aarch64-linux-gnu/libgtest.a和/usr/lib/aarch64-linux-gnu/libgtest_main.a

上面的错误信息表明发现了/usr/lib/aarch64-linux-gnu/libgtest.a,不知为何就是发现不了/usr/lib/aarch64-linux-gnu/libgtest_main.a,但是/usr/lib/aarch64-linux-gnu/pkgconfig/gtest_main.pc  /usr/lib/aarch64-linux-gnu/libgtest_main.a和/usr/lib/aarch64-linux-gnu/pkgconfig/gtest.pc /usr/lib/aarch64-linux-gnu/libgtest.a 一样都是存在的,按理能发现libgtest 也应该能发现libgtest_main.a,但是就是莫名其妙地报上面的错。

发现在同样的程序在另一个Orin上编译时却能顺利成功,但是对比了一下两个环境的libgtest-dev和cmake版本都是一模一样的!抱着试试看的心态继续对比了一下两个环境下cmake用于发现GTest模块的文件: /usr/share/cmake-3.16/Modules/FindGTest.cmake, 没想到发现两个环境下这个文件的内容还真不一样,能顺利编译成功的环境下的FindGTest.cmake里内容要多得多!看来虽然cmake版本一样,但是支持的module还是有差别,于是再仔细对比了一下cmake相关的包发现有差异:

ii  cmake                                        3.16.3-1ubuntu1                     arm64        cross-platform, open-source make system
ii  cmake-data                                   3.16.3-1ubuntu1                     all          CMake data files (modules, templates and documentation)
ii  cmake                                        3.16.3-1ubuntu1.20.04.1             arm64        cross-platform, open-source make system
ii  cmake-data                                   3.16.3-1ubuntu1.20.04.1             all          CMake data files (modules, templates and documentation)

很显然,编译报错的环境下的cmake和cmake-data的版本号里没有20.04.1,于是试着安装cmake:

sudo apt-get install cmake

出现了需要升级cmake 和cmake-data的提示!这表明编译出错的环境下的cmake 3.16.3还不是最新的!

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  cmake-data
Suggested packages:
  cmake-doc ninja-build
The following packages will be upgraded:
  cmake cmake-data
2 upgraded, 0 newly installed, 0 to remove and 354 not upgraded.
Need to get 4,725 kB of archives.
After this operation, 38.9 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://mirrors.aliyun.com/ubuntu-ports focal-updates/main arm64 cmake arm64 3.16.3-1ubuntu1.20.04.1 [3,112 kB]
Get:2 http://mirrors.aliyun.com/ubuntu-ports focal-updates/main arm64 cmake-data all 3.16.3-1ubuntu1.20.04.1 [1,613 kB]
Fetched 4,725 kB in 1s (3,622 kB/s)   
debconf: delaying package configuration, since apt-utils is not installed
(Reading database ... 214219 files and directories currently installed.)
Preparing to unpack .../cmake_3.16.3-1ubuntu1.20.04.1_arm64.deb ...
Unpacking cmake (3.16.3-1ubuntu1.20.04.1) over (3.16.3-1ubuntu1) ...
Preparing to unpack .../cmake-data_3.16.3-1ubuntu1.20.04.1_all.deb ...
Unpacking cmake-data (3.16.3-1ubuntu1.20.04.1) over (3.16.3-1ubuntu1) ...
Setting up cmake-data (3.16.3-1ubuntu1.20.04.1) ...
Setting up cmake (3.16.3-1ubuntu1.20.04.1) ...
Processing triggers for man-db (2.9.1-1) ...

升级完后,cmake的版本里也有了ubuntu20.04.1,然后编译程序就不再报错了!难怪两个环境下的/usr/share/cmake-3.16/Modules/FindGTest.cmake有差异,cmake-data里包括module的文件,cmake-data版本不一样,module的文件内容自然有差异了。

另外记录一个小问题,在编译gtest测试应用程序时,在脚本里加了个-shared

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -shared -fPIC")

忘了删除,编译出来的测试应用程序在运行时总是崩溃:

CMake Error at /usr/share/cmake-3.16/Modules/GoogleTestAddTests.cmake:40 (message):
  Error running test executable.

    Path: '/home/nvidia/workspace/alg_common-development/build/tests/rb_test/rb_test'
    Result: Segmentation fault
    Output:

用gdb看又看不出和测试应用程序本身有关:

Program received signal SIGSEGV, Segmentation fault.
0x0000fffff7ec0438 in testing::internal::FormatCxxExceptionMessage(char const*, char const*) ()
(gdb) bt
#0  0x0000fffff7ec0438 in testing::internal::FormatCxxExceptionMessage(char const*, char const*) ()

应该是gtest主程序googletest-1.10.0/googletest/src/gtest_main.cc里问题,但是重装了libgtest-dev和 googletest 

ii  libgtest-dev:arm64                           1.10.0-2                            arm64        Google's framework for writing C++ tests
ii  googletest                                   1.10.0-2                            all          Google's C++ test framework sources

后仍然没效果,后来再想想莫非主程序装载测试应用时有问题,检查了一下CMakeLists.txt脚本,去掉了之前实验时给CMAKE_CXX_FLAGS加的 -shared后再编译后运行,测试程序立即可以正常运行了!

你可能感兴趣的:(CMake,GTest,gtest_main,gooletest)