如何使用cmake链接静态库和谷歌测试

时间:2016-02-28 10:31:43

标签: c++ cmake googletest

我试图将我的项目与谷歌C ++测试框架联系起来。我有这个目录结构:

-- GeKon --- gekon (where my project is)
         |-- tests -- lib (google test)

项目结构应该从CLion的创建者复制这个例子: Calendar

CMakeFiles有这样的内容:

root CMakeFile

cmake_minimum_required (VERSION 2.8)
project(Gekon)
include_directories(gekon)
add_subdirectory(gekon)
add_subdirectory(tests)

gekon CMakeFile (省略了几行以使其更短)

project (gekon)
find_package(OpenCV REQUIRED)
set(HEADER_FILES ...)
set(SOURCE_FILES gk_functions.cpp)
add_executable(gekon_run main.cpp ${SOURCE_FILES} ${HEADER_FILES})
target_link_libraries(gekon_run ${OpenCV_LIBS})
add_library(gekon STATIC ${SOURCE_FILES} ${HEADER_FILES})

测试CMakeFile

project(gekon_tests)
add_subdirectory(lib)
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
find_package(OpenCV REQUIRED)
add_executable(runGekonTests first_test.cpp)
target_link_libraries(runGekonTests gtest gtest_main)
target_link_libraries(runGekonTests gekon)
target_link_libraries(runGekonTests ${OpenCV_LIBS})

但是我仍然从链接器收到错误,它找不到对该函数的引用:

CMakeFiles/runGekonTests.dir/first_test.cpp.o: In function `fitness_mse_test_test_equal_Test::TestBody()':
/path/tests/first_test.cpp:32:undefined reference to `gekon::fitness_mse(gekon::tr_sample_t, cv::Mat)'
collect2: error: ld returned 1 exit status
tests/CMakeFiles/runGekonTests.dir/build.make:135: recipe for target 'tests/runGekonTests' failed
gmake[3]: *** [tests/runGekonTests] Error 1

我也尝试过像StackOwerflow这样的建议: How do I tell CMake to link in a static library in the source directory? 但仍然没有成功。

我该怎么做才能让它发挥作用?

项目源代码在这里:

Project source on GitHub

cmake version 3.4.1
gcc version 5.3.1 

解决方案

问题既不在cmakefile中也不在测试中。我的* .cpp文件错了。而不是namespace gekon {...}我使用using namespace gekon

0 个答案:

没有答案