我最近在一个项目中添加了一个新特性和相应的单元测试.项目的 struct 如下:
Sim
└── models
├── otherModels
└── myModel
├── CMakeLists.txt
├── model.cpp
├── model.h
├── test
│ ├── CMakeLists.txt
│ ├── geometryTest.cpp
│ └── geometryTest.h
└── util
├── CMakeLists.txt
├── geometry.cpp
├── geometry.h
└── probability.h
Summarize the problem
我的目标是能够构建上述项目,并在Windows和Linux上执行单元测试.
实际效果
在Windows上,我能够很好地构建项目并运行单元测试.
在Linux上,我在构建时得到一个错误,说无法找到正在测试的库:
/home/e40056742/projects/xxxx/build/make-Release/Sim/bin/modelTests: error while loading shared libraries: libmathUtil.so: cannot open shared object file: No such file or directory
CMake Error at /projs/xxxx/cmake-3.18.2/share/cmake-3.18/Modules/GoogleTestAddTests.cmake:77 (message):
Error running test executable.
Path: '/home/e40056742/projects/xxxx/build/make-Release/Sim/bin/modelTests'
Result: 127
Output:
Call Stack (most recent call first):
/projs/xxxx/cmake-3.18.2/share/cmake-3.18/Modules/GoogleTestAddTests.cmake:173 (gtest_discover_tests_impl)
make[2]: *** [Sim/bin/modelTests] Error 1
make[2]: *** Deleting file `Sim/bin/modelTests'
make[1]: *** [Sim/src/models/myModel/test/CMakeFiles/modelTests.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
预期结果
能够在Linux上构建.
What I've tried
-
我在谷歌上搜索了如何在Linux上找到文件,并验证了该文件是否存在:
find . -name libmathUtil.so >>> ./build/make-Release/Sim/lib/libmathUtil.so
-
如果我注释掉
add_subdirectory(test)
,我验证了所有东西都构建在Linux上 -
我try 了这里发布的解决方案,回答了一个不同的(但我希望是相关的)问题:How to add linker directories to cmake gtest_discover_tests
-
我将CMake RPATH设置添加到我的测试目录的CMakeLists.txt中,如另一个答案所示:https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling#always-full-rpath
-
我try 正确设置CMakeLists.txt文件,如下所示:
Top level CMake for my working directory个
set( target myCoolModel )
set( sources
model.cpp
)
set( headers
model.h
)
add_library( ${target} ${sources} ${headers} )
target_link_libraries( ${target}
${PROJECT_LIBRARIES}
mathUtil
)
# Group the target library into an IDE folder
set_target_properties( ${target} PROPERTIES FOLDER ${PROJECT_FOLDER} )
# Add the utility and test subdirectory
enable_testing()
add_subdirectory( util )
add_subdirectory( test )
And in the 100 sub-directory:
set( target mathUtil)
set( sources geometry.cpp )
set( headers geometry.h probability.h)
add_library( ${target} ${sources} ${headers} )
target_link_libraries( ${target} genMath::genMath )
target_compile_options( ${target} PRIVATE ${PROJECT_CXX_FLAGS} )
target_include_directories( ${target}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} )
And the 100 directory:
enable_testing()
# Set target name, and dependencies
set( target modelTests )
set( sources geometryTest.cpp )
set( headers geometryTest.h)
# Find Google Test
find_package( GTest REQUIRED )
# Make test executable.
add_executable( ${target} ${sources} ${headers} )
target_link_libraries(${target}
mathUtil
GTest::gtest_main
)
# Load GoogleTest and add these tests to the suite
include(GoogleTest)
gtest_discover_tests(${target})
# File unit tests into the project test folder (makes it easier to find in solution explorer)
set( PROJECT_FOLDER ${PROJECT_FOLDER}/test )
set_target_properties( ${target} PROPERTIES FOLDER ${PROJECT_FOLDER} )