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后再编译后运行,测试程序立即可以正常运行了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,这是一个关于使用 CMake 生成可供其他 CMake 文件 `find_package()` 的文件的问题。下面是一个简单的步骤: 1. 编写 CMakeLists.txt 文件,定义要生成的文件。 ```cmake # 定义要生成的文件名称和源文件 add_library(mylib mylib.cpp) # 定义头文件路径 target_include_directories(mylib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) # 导出文件的头文件和文件 include(GNUInstallDirs) install(TARGETS mylib EXPORT mylib-config LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install(EXPORT mylib-config NAMESPACE mylib:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/mylib FILE mylib-config.cmake) ``` 2. 在 CMakeLists.txt 文件中设置文件的属性。 ```cmake # 设置文件属性 set_target_properties(mylib PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION 1 PUBLIC_HEADER include/mylib.h ) ``` 3. 生成配置文件 ```cmake # 生成配置文件 include(CMakePackageConfigHelpers) write_basic_package_version_file( "${CMAKE_CURRENT_BINARY_DIR}/mylib-config-version.cmake" VERSION ${PROJECT_VERSION} COMPATIBILITY AnyNewerVersion ) configure_package_config_file( "${CMAKE_CURRENT_LIST_DIR}/mylib-config.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/mylib-config.cmake" INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/mylib" ) # 安装配置文件 install( FILES "${CMAKE_CURRENT_BINARY_DIR}/mylib-config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/mylib-config-version.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/mylib" ) ``` 4. 在其他 CMakeLists.txt 文件中使用生成的文件。 ```cmake # 查找文件 find_package(mylib REQUIRED) # 添加文件路径 link_directories(${mylib_LIBRARY_DIRS}) # 添加文件 add_executable(myapp main.cpp) target_link_libraries(myapp ${mylib_LIBRARIES}) ``` 以上就是一个简单的使用 CMake 生成可供其他 CMake 文件 `find_package()` 的文件的步骤。希望能够帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Arnold-FY-Chen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值