【问题标题】:Cmake and GtestCmake 和 Gtest
【发布时间】:2016-11-04 17:05:18
【问题描述】:

我想使用 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 文件,但是当我尝试测试包含 3rd 方库的项目时,我会遇到不同的错误。

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

【问题讨论】:

  • 您可能需要查看this answer 以了解将 gtest 引入您的构建的替代方法,而不是该教程中的方法。回答链接到的文章在 github 上也有一个相关项目,其中包含一个完整的工作示例。至于你的链接问题,如果你展示了你得到的实际错误,那将允许对你的潜在问题进行更具体的诊断。您的项目似乎正在从项目外部的目录中提取其所有源等,这也很奇怪。

标签: c++ cmake googletest


【解决方案1】:

如果您想链接到第三方库,您通常首先:

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

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

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

在这种情况下,您可能希望将您的待测单元定义为 OBJECT 类型库,然后不要对这些对象库执行 target_link_libraries(),而是使用以下语法将对象作为可执行文件的源代码的一部分:@ 987654325@(cmake 生成器表达式)。

因此,对于依赖于第 3 方库的单元,例如 Qt5 Core,您将拥有像这样的 sn-ps:

# 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)

【讨论】:

    猜你喜欢
    • 2015-03-15
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 2020-08-19
    • 2018-10-02
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    相关资源
    最近更新 更多