Cmake 和 Gtest

Cmake and Gtest

我想使用 Google C++ 测试,我完全是 cmake 和 gtest 的初学者。

我有一个名为 Filter 的 class,它使用名为 jane 的 3d 派对库。

对于这种情况,我有一个 cmakeFile 可以很好地构建我的项目,如下所示:

cmake_minimum_required(VERSION 3.1.2)
project(Filter)
include(../../../cmake/CMakeMacros.txt)
set_variables()
#add 3rdparty libraries
add_jane()
#add framework libraries
add_framework_libs(
ip/Image
)
include_directories(
../include
${FW_INCLUDE_DIRS}
)

#set project's source and include files
set(INCS
../include/${PROJECT_NAME}.h
../include/${PROJECT_NAME}.tpp
../include/FilterMask.h
)

set(SRCS
../src/${PROJECT_NAME}.cpp
../src/FilterMask.cpp
)

#set link directories
link_directories(
${FW_LIBRARY_DIRS}
)

#build project as static library (*.lib)
add_library(${PROJECT_NAME} STATIC
${INCS}
${SRCS}
)
#link libraries against project
target_link_libraries( ${PROJECT_NAME}
${FW_LIBRARIES}
)

#if a test executable should be build
if(Test_BUILD_EXAMPLES)
#build test executable
add_executable(${PROJECT_NAME}Test
    ../src/main.cpp
)
#link library against executable
target_link_libraries(${PROJECT_NAME}Test
    ${PROJECT_NAME}
)
endif(Test_BUILD_EXAMPLES)

而且我已经在 https://github.com/snikulov/google-test-examples with this cmake file https://github.com/snikulov/google-test-examples/blob/master/CMakeLists.txt 上阅读了这个简单的教程并尝试再次构建我的项目以将这些 cmake 文件组合在一起(可能以非常愚蠢的方式)但我几天以来无法实现它。

问题是,当我想测试一个只有头文件的简单项目时,我可以使用这个 cmake 文件,但是当我尝试测试包含第 3 方库的项目时,我 运行 进入不同的错误。

谁能告诉我如何编辑正确的 cmake 文件以使用 cmake 文件通过 googleTest 测试我的项目!?

如果您想 link 针对第 3 方库,您通常首先:

  1. find_package() 或使用 pkg 配置支持来检查构建主机上的库是否可用并获取对它的引用。
  2. target_link_libraries()
  3. 中包含步骤 #1 中的引用

这就是您需要为第 3 方库做的事情。对于您自己想要测试的代码,您可能希望将其全部放入您自己的库中,然后 link 针对这些代码进行测试。

如果您有多个测试可执行文件来将每个测试套件分离并隔离到其自己的二进制文件中,您可能需要一种替代技术来避免过度 linking 并将测试套件中的代码限制为下面的实际单元测试。 (当您的代码库不断变化并且仅部分构建但您仍然希望检查哪些构建继续通过相关测试时,这也非常有用。)

在这种情况下,您可能希望将被测单元定义为 OBJECT 类型库,而不是对这些对象库执行 target_link_libraries(),而是将对象作为源的一部分包含在使用此语法的可执行文件:$<TARGET_OBJECTS:NameOfObjLibHere>(cmake 生成器表达式)。

因此,对于依赖于第 3 方库(例如 Qt5 Core)的单元,您将拥有如下代码片段:

# define the dependency on 3rd party project Qt5, (sub)component: Core, Test)
set(MY_QT_VERSION "5.4.0")
find_package(Qt5 ${MY_QT_VERSION} REQUIRED COMPONENTS Core CONFIG)

# define the object lib for a unit to be tested (Item1)
set(item1_srcs item1.cpp util1.cpp)
add_library(Item1 TYPE OBJECT ${item1_srcs})

# ensure that necessary compiler flags are passed 
# when building "Item1" separately
# note that PRIVATE may also be INTERFACE or PUBLIC
# read the cmake docs on target_include_*** to determine which applies.
# you probably want to hide this behind a convenience macro.
target_include_directories(Item1 PRIVATE $<TARGET_PROPERTY:Qt5::Core,INTERFACE_INCLUDE_DIRECTORIES>)
target_compile_options(Item1 PRIVATE $<TARGET_PROPERTY:Qt5::Core,INTERFACE_COMPILE_OPTIONS>)

# find the unit testing framework (dependency)
# this sample uses Qt5 Test (Qt5::Test) but you could use GTest, too
find_package(Qt5 ${MY_QT_VERSION} REQUIRED COMPONENTS Test CONFIG)

# define an executable which contains test sources + unit under test
# link against the testing framework (obviously) as per normal
# note the Qt5::Core dependency here: remember Item1 depends on Qt5::Core (!)
set(test_item1_srcs, test_item1.cpp $<TARGET_OBJECTS:Item1>)
add_executable(test_item1 ${test_item1_srcs)
target_link_libraries(test_item1 Qt5::Core Qt5::Test)

# inform cmake/ctest integration about our test
# so it knows to execute it during `make test` phase.
# and other cmake/ctest integration falls into place as well, possibly
add_test(test_item1 test_item1)