如何使用cmake链接静态库和google测试 我试图把我的项目与谷歌C++测试框架联系起来。我有以下目录结构: -- GeKon --- gekon (where my project is) |-- tests -- lib (google test)
项目结构应该从CLion的创建者那里复制这个示例: CMAKE文件包含以下内容: 根CMakeFile如何使用cmake链接静态库和google测试 我试图把我的项目与谷歌C++测试框架联系起来。我有以下目录结构: -- GeKon --- gekon (where my project is) |-- tests -- lib (google test),c++,cmake,googletest,C++,Cmake,Googletest,项目结构应该从CLion的创建者那里复制这个示例: CMAKE文件包含以下内容: 根CMakeFile cmake_minimum_required (VERSION 2.8) project(Gekon) include_directories(gekon) add_subdirectory(gekon) add_subdirectory(tests) project(gekon_tests) add_subdirectory(lib) include_directories(${gtes
cmake_minimum_required (VERSION 2.8)
project(Gekon)
include_directories(gekon)
add_subdirectory(gekon)
add_subdirectory(tests)
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})
gekon CMakeFile
(为了缩短,省略了几行)
测试CMakeFile
cmake_minimum_required (VERSION 2.8)
project(Gekon)
include_directories(gekon)
add_subdirectory(gekon)
add_subdirectory(tests)
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的一些建议,比如:
但仍然没有成功
我该怎么做才能让它工作
项目源代码如下:
解决方案
问题既不在CMAKE文件中,也不在测试中。我的*.cpp文件出错。我使用的不是
namespace gekon{…}
而是使用namespace gekon
您是否尝试在单个命令中传递链接库<代码>目标链接库(runGekonTests gekon gtest gtest_main${OpenCV_LIBS})。看起来更像是与链接您自己的库而不是gtest相关的错误。另外,我在您的回购协议中加入了一个简短的llook,您可以将gtest作为git子模块加入:git子模块addhttps://github.com/google/googletest.git
我做了,但还是一样。是的,问题在于链接我的库,也许我选择了一个令人困惑的名字。不管怎样,谢谢你的子模块,我用它替换了自述文件。你试过用一个命令传递链接库吗<代码>目标链接库(runGekonTests gekon gtest gtest_main${OpenCV_LIBS})。看起来更像是与链接您自己的库而不是gtest相关的错误。另外,我在您的回购协议中加入了一个简短的llook,您可以将gtest作为git子模块加入:git子模块addhttps://github.com/google/googletest.git
我做了,但还是一样。是的,问题在于链接我的库,也许我选择了一个令人困惑的名字。无论如何,谢谢你的子模块,我用它替换了自述文件。