我的项目中有一个目录树:
/project
/build
/src
main.cpp
student.cpp
/include
student.hpp
/test
main_test.cpp
CMakeLists.txt
CMakeLists.txt我还有我的gtest和gmock库:
/home/karol/Google
/gtest
/gmock
/lib我想知道是否应该将project/CMakeLists.txt移到src/目录中?
我的目标是在编译二进制文件或单元测试之间做出选择。我想知道CMakeLists应该是什么样子才能实现这一点。
发布于 2014-08-29 16:36:08
在您的project/CMakeLists.txt中添加:
add_subdirectory (test)
add_custom_target (testing)
add_dependencies (testing main_test)在您的project/test/CMakeList.txt中添加:
add_executable (main_test EXCLUDE_FROM_ALL main_test.cpp)现在,如果您只输入make,将构建二进制文件,但如果您输入make testing,则将构建单元测试。
发布于 2014-08-29 15:27:39
您的CMakeLists.txt文件应该放在项目的根目录中,而不是主要源代码的文件夹中。您可以通过CMake直接指定源代码和/或项目。
无论如何,如果测试/CMakeLists.txt是另一个项目(或者gtest的项目),你不应该接触它,而应该从CMake中add_subdirectory它。
如果该文件包含可重用函数,请查看this answer
发布于 2014-08-29 15:35:58
在主project/CMakeLists.txt中,只需使用以下命令:
add_subdirectory( test )它将包含test目录中的所有目标,并且在构建项目时可以访问这些目标。
现在,假设您的gmock目录在项目层次结构之外,您应该在project/test/CMakeLists.txt中执行以下操作
add_subdirectory( /home/karol/Google/gmock gmock )
include_directories(
${gtest_SOURCE_DIR}/include
${gmock_SOURCE_DIR}/include
)
add_executable(
test_exec
test.cpp # list the cpp files with your tests
)
target_link_libraries(
test_exec
gmock_main
)https://stackoverflow.com/questions/25563336
复制相似问题