C++ CMakeLists+;gtest
我的项目中有一个目录树:C++ CMakeLists+;gtest,c++,cmake,googletest,gmock,C++,Cmake,Googletest,Gmock,我的项目中有一个目录树: /project /build /src main.cpp student.cpp /include student.hpp /test main_test.cpp CMakeLists.txt CMakeLists.txt 此外,我的gtest和gmock库位于: /home/karol/Google /gtest
/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必须是什么样子才能实现这一点。您的CMakeLists.txt文件应该保存在项目的根目录中,而不是主要来源的文件夹中。您可以直接通过CMake指定源和/或项目 无论如何,如果test/CMakeLists.txt是另一个项目(或gtest的项目),您不应该触摸它,而是应该从CMake中触摸它
如果该文件包含可重用函数,请查看主
项目/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
)
在您的项目/CMakeLists.txt
中添加:
add_subdirectory (test)
add_custom_target (testing)
add_dependencies (testing main_test)
add_executable (main_test EXCLUDE_FROM_ALL main_test.cpp)
在您的项目/test/CMakeList.txt
中添加:
add_subdirectory (test)
add_custom_target (testing)
add_dependencies (testing main_test)
add_executable (main_test EXCLUDE_FROM_ALL main_test.cpp)
现在,如果您只键入make
将生成二进制文件,但是如果键入make testing
将生成单元测试。您所面临的问题尚不清楚。您可以选择任何一种方式:将CMakeLists.txt保留在project/中,将其移动到project/src/或在project/src/中创建一个额外的CMakeLists.txt。根据您试图实现的目标,这些方法中的每一种都可能是最佳解决方案。不确定include
是否是添加CMakeLists.txt的解决方案,而是针对*。cmake
files@PiotrS. -> 文件|模块。无论如何,add_子目录是一个更好的选择(include应该针对可重用函数)。我会编辑这个,但我不知道这些文件的性质。