我想使用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)
并且我已经使用此cmake文件https://github.com/snikulov/google-test-examples阅读了https://github.com/snikulov/google-test-examples/blob/master/CMakeLists.txt上的这个简单教程,并尝试再次构建我的项目以将这些cmake文件组合在一起(可能非常愚蠢)但我可以从那以后就没有实现它。
问题在于,当我想用一个头文件测试一个简单的项目时,我可以使用这个cmake文件,但是当我尝试测试包含第三方库的项目时,我遇到了不同的错误。
有人可以告诉我如何使用cmake文件编辑正确的cmake文件以使用googleTest测试我的项目吗?
答案 0 :(得分:2)
如果您想首先链接第三方图书馆:
find_package()
或使用pkg配置支持检查构建主机上的库是否可用,并获取对它的引用。target_link_libraries()
这就是你需要为你的第三方lib做的事情。对于您想要测试的自己的代码,您可能希望将它们全部放在自己的库中,然后将测试与这些代码链接起来。
如果您有多个测试可执行文件要分开&将每个测试套件隔离到自己的二进制文件中,您可能需要一种替代技术来避免过度链接,并将测试套件中的代码限制为实际的测试单元。 (当您的代码库不断变化且部分构建时,这也非常有用,但您仍然希望检查哪些构建继续通过相关测试。)
在这种情况下,您可能希望将测试中的单位定义为OBJECT
类型库,然后针对那些对象库执行target_link_libraries()
,而不是将对象作为可执行文件源的一部分包含在内语法:$<TARGET_OBJECTS:NameOfObjLibHere>
(cmake生成器表达式)。
所以对于依赖第三方库的单位,例如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)