2016-07-02 37 views
0

我想使用谷歌C++测试,我完全是初学者cmake和gtest。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阅读https://github.com/snikulov/google-test-examples这个简单的教程,并试图重新建立我的项目相结合这些cmake文件在一起(可能非常愚蠢),但是我无法实现它。

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

有人可以告诉我如何编辑一个正确的cmake文件来测试我的项目与googleTest使用cmake文件!

+0

你可能想看看[这个答案](HTTP: //stackoverflow.com/a/31622855/1938798),为您的构建带来gtest的替代方法,而不是该教程中的方法。通过链接到的文章还有一个关于github的相关项目,它有一个完整的工作示例。至于你的链接问题,如果你显示了你得到的实际错误,这将允许更具体的诊断你的根本问题。同样很奇怪的是,您的项目似乎正在从项目外的其他目录中提取等。 –

回答

1

如果您想对第三方库,你通常第一链接:

  1. find_package()或使用pkg配置支持,以检查库可构建主机上,拿起一个参考吧。
  2. 包括从步骤#1 target_link_libraries()

参考所以这就是你需要为你的第三方的lib做什么。对于您想要在测试中使用的代码,您可能希望将其全部放入自己的库中,然后将测试与这些测试相关联。

如果您有多个测试可执行文件来分隔&将每个测试套件分隔成它自己的二进制文件,您可能需要一种替代技术来避免过度链接并将测试套件内的代码限制为实际待测试的单元。 (当你的代码库不稳定并且只是部分构建时,这也非常有用,但是你仍然希望检查哪些构建继续通过相关测试。)

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

所以在依赖于第三方的lib单位的情况下,说,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)