我想使用谷歌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/blob/master/CMakeLists.txt阅读了这个关于cmake文件的简单教程,并试图再次构建我的项目,以便将这些cmake文件组合在一起(可能是以非常愚蠢的方式),但我几天来一直无法实现它。
问题是,当我想用一个头文件测试一个简单的项目时,我可以使用这个cmake文件,但是当我尝试测试包含第三方库的项目时,我会遇到不同的错误。
有人能告诉我如何编辑一个正确的cmake文件来用googleTest测试我的项目吗?
发布于 2016-07-02 12:21:42
如果要链接到第三方库,通常首先:
find_package()或使用pkg配置支持检查库在构建主机上可用,并获取对它的引用。target_link_libraries()中包含步骤1中的引用这就是你需要为你的第三党解放所做的。对于您想要进行测试的您自己的代码,您可能希望将其全部放在您自己的库中,然后将您的测试链接到这些库中。
如果您有多个测试可执行程序来将每个测试套件分离和隔离到它自己的二进制文件中,那么您可能需要另一种技术来避免过度链接测试套件中的代码,并将其限制在实际被测试单元上。(当您的代码库不断变化且只构建部分时,这也非常有用,但您仍然希望检查哪些构建继续通过相关测试。)
在这种情况下,您可能希望将被测试的单元定义为OBJECT类型库,而不是对那些对象库执行target_link_libraries(),而是将对象作为使用以下语法的可执行文件源的一部分:$<TARGET_OBJECTS:NameOfObjLibHere> (cmake生成器表达式)。
因此,对于依赖第三方库(例如,Qt5核心)的单元,应该有如下代码片段:
# 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)https://stackoverflow.com/questions/38159747
复制相似问题