c++ - CMakeLists + gtest

标签 c++ cmake googletest googlemock

我的项目中有一个目录树:

/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 将如何实现这一目标。

最佳答案

在你的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 将构建单元测试。

关于c++ - CMakeLists + gtest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25563336/

相关文章:

c++ - 使用 next_permutation 置换类 vector

c++ - 应该接受不同输入参数的纯虚函数——应该如何实现?

c++ - 使用 SDL2 和 MinGW 诊断 DLL 问题

c++ - FFmpeg H264解析

cmake - 移动目录后编辑与删除 CMakeCache.txt

cmake - 为 Raspbian 交叉编译 cpp-netlib 时出错

c++ - 访问 Google Test 参数化测试用例 (`TEST_P` 中的测试信息)

c++ - 测试相等运算符

c++ - 链接器没有链接我自己的静态库

c++ - googletest:死亡测试:单个测试中有多个 expect_exit