c++ - Cmake 和 Gtest

标签 c++ cmake googletest

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

我有一个名为 Filter 的类,它使用一个名为 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 的这个简单教程使用此 cmake 文件 https://github.com/snikulov/google-test-examples/blob/master/CMakeLists.txt并尝试再次构建我的项目以将这些 cmake 文件组合在一起(可能以非常愚蠢的方式)但我几天以来无法实现它。

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

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

最佳答案

如果你想链接到第 3 方库,你通常首先:

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

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

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

在这种情况下,您可能希望将被测单元定义为 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)

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

相关文章:

c++ - 字符串是否在其构造函数中复制数据

c++ - 如何将 -Wno-unused-private-field 添加到一个文件中?

c++ - C++ 标准库中采用右值引用的构造函数

qt - CMake 宏仅运行 lupdate

c++ - 使用临时的 gtest fixture 引用类成员的初始化

c++ - 完全模拟缺失的不同内置类型(特别是 : char16_t and char32_t)

c++ - 使用 CMake、DSO-Link-Change 链接失败

c++ - 将 CMake 与多个编译器一起用于同一语言

c++ - Google 测试 - 将数组作为 void* 参数进行比较

c++ - Google 测试中的 BOOST_CHECK_EQUAL_COLLECTIONS