2

我开始使用 CMake 来使用 Qt 创建一个项目并使用 Google Test 对其进行测试。目前,我成功地找到了编译和链接所有必需库的方法。但是,我找不到将源链接到具有以下项目结构的测试文件的方法:

root
|
+-- CMakeLists.txt
+-- src
| |
| +-- CMakeLists.txt
| +-- MyClass.h
| +-- MyClass.cpp
|
+-- test
| |
| +-- CMakeLists.txt
| +-- MyClassTest.cpp
|
+-- lib
  |
  +-- gtest-1.6.0
    |
    +-- CMakeLists.txt

根 CMakeLists.txt 包含 gtest、src 和 test 文件夹的 add_subdirectory。我已成功编译并运行“Hello world”应用程序和简单的 EXPECT_TRUE(true) 测试,以检查每个部分是否正确编译。不幸的是,我找不到将源文件包含到测试中的方法。是否可以使用以下项目结构?

PS我知道可以将我的源代码编译为库并将其链接到测试,但我不喜欢这种方法,因为它更适合集成测试,而不是单元测试......

编辑:向树中添加了类名

4

2 回答 2

2

您可以在根 CMakeLists.txt 级别添加一个全局变量:

set(ALL_SRCS CACHE INTERNAL "mydescription" FORCE)

在第一个 add_subdirectory(src) 中,您可以执行以下操作:

set(ALL_SRCS ${ALL_SRCS} blabla.cpp CACHE INTERNAL "description")

在 add_subdirectory(test) 中,您继续:

set(ALL_SRCS ${ALL_SRCS} bla_test.cpp CACHE INTERNAL "description")

You can then do, add_executable, or library or whatever, with all your sources files.

EDIT: add trick for global variables in CMake.

于 2012-12-10T09:14:42.070 回答
1

在根 CMakeLists.txt 中,您可以添加一个include_directories(src)This 也将被测试使用。您可以做的另一件事是在测试 CMakeLists.txt 中添加一个include_directories(${<projectName>_SOURCE_DIR})where projectNameproject(myproj)在 src/CMakeLists.txt 中使用指定的名称(当然,如果您在其中指定了一个项目。还要检查有关project的文档

于 2012-12-10T08:51:10.453 回答