我有一个CMAKE项目,其中包含两个子目录,图书馆和测试。当枚举测试时,gtest_discover_tests运行测试可执行文件,但由于测试可执行文件位于被测试库的兄弟目录中,因此似乎找不到它。如果我通过终端运行它并设置LD_LIBRARY_PATH,它运行得很好。有没有CMAKE方法来为编译过程设置LD_LIBRARY_PATH?我可以在我的IDE中强制它,但这对于试图编译它的其他人来说是脆弱的。
foo/CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(foo)
add_subdirectory(library)
add_subdirectory(tests)
foo/library/CMakeLists.txt
add_library(foo SHARED foo.cpp foo.h)
foo/tests/CMakeLists.txt
enable_testing()
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
FetchContent_MakeAvailable(googletest)
include_directories(../library)
add_executable(foo_test foo_test.cpp)
target_link_libraries(foo_test PRIVATE gtest_main foo)
include(GoogleTest)
gtest_discover_tests(foo_test)
精确误差输出
[1/1 5.1/sec] Linking CXX executable tests/foo_test
FAILED: tests/foo_test tests/foo_test[1]_tests.cmake
: && /usr/lib/ccache/clang++-11 --target=x86_64-pc-linux-gnu --sysroot=/ -g tests/CMakeFiles/foo_test.dir/foo_test.cpp.o -o tests/foo_test -L/home/adam/workspace-qt/foo/tests/../library -Wl,-rpath,home/adam/workspace-qt/foo/tests/../library:home/adam/workspace-qt/build-foo-Clang_Desktop-Debug/library lib/libgtest_maind.a library/libfoo.so mbedtls/library/libmbedtls.a lib/libgtestd.a -lpthread mbedtls/library/libmbedx509.a mbedtls/library/libmbedcrypto.a && cd /home/adam/workspace-qt/build-foo-Clang_Desktop-Debug/tests && /home/adam/Qt/Tools/CMake/bin/cmake -D TEST_TARGET=foo_test -D TEST_EXECUTABLE=/home/adam/workspace-qt/build-foo-Clang_Desktop-Debug/tests/foo_test -D TEST_EXECUTOR= -D TEST_WORKING_DIR=/home/adam/workspace-qt/build-foo-Clang_Desktop-Debug/tests -D TEST_EXTRA_ARGS= -D TEST_PROPERTIES= -D TEST_PREFIX= -D TEST_SUFFIX= -D NO_PRETTY_TYPES=FALSE -D NO_PRETTY_VALUES=FALSE -D TEST_LIST=foo_test_TESTS -D CTEST_FILE=/home/adam/workspace-qt/build-foo-Clang_Desktop-Debug/tests/foo_test[1]_tests.cmake -D TEST_DISCOVERY_TIMEOUT=5 -D TEST_XML_OUTPUT_DIR= -P /home/adam/Qt/Tools/CMake/share/cmake-3.19/Modules/GoogleTestAddTests.cmake
/home/adam/workspace-qt/build-foo-Clang_Desktop-Debug/tests/foo_test: error while loading shared libraries: libfoo.so: cannot open shared object file: No such file or directory
CMake Error at /home/adam/Qt/Tools/CMake/share/cmake-3.19/Modules/GoogleTestAddTests.cmake:77 (message):
Error running test executable.
Path: '/home/adam/workspace-qt/build-foo-Clang_Desktop-Debug/tests/foo_test'
Result: 127
Output:
Call Stack (most recent call first):
/home/adam/Qt/Tools/CMake/share/cmake-3.19/Modules/GoogleTestAddTests.cmake:173 (gtest_discover_tests_impl)
ninja: build stopped: subcommand failed.
15:56:10: The process "/home/adam/Qt/Tools/CMake/bin/cmake" exited with code 1.
Error while building/deploying project foo (kit: Clang-Desktop)
When executing step "Build"
15:56:10: Elapsed time: 00:01.
编辑:
与@Tsyvarev的讨论表明,QtCreator可能没有在RPATH/RUNPATH中放置完全限定的路径。(即'home/...'而不是'/home/...')。
1条答案
按热度按时间km0tfn4u1#
我遇到过类似的问题。我的解决方法是在您的案例中为
foo
库添加RPATH。那么就不需要为gtest设置LD_LIBRARY_PATH
。这可以通过在安装foo
库时设置CMAKE_INSTALL_RPATH
来完成。我类似的职位在这里:https://stackoverflow.com/a/66394189/19773052另一个解决方法来自这篇文章:CMAKE Linking dynamic library to module, but not showing as link dependency。更优雅的方式是
target_link_options(foo_test PRIVATE -Wl,--as-needed)
。但这可能会包括不必要的链接到可执行文件的信息。