【问题标题】:CMake: Unable to link with GMockCMake:无法与 GMock 链接
【发布时间】:2020-02-03 13:43:58
【问题描述】:

我正在尝试在单元测试中使用 GMock。但是链接失败并出现下一个错误:

In function `testing::internal::linked_ptr_internal::depart()':
/home/user/googletest/googletest/include/gtest/internal/gtest-linked_ptr.h:127: undefined reference to `testing::internal::g_linked_ptr_mutex'
In function `testing::internal::linked_ptr_internal::join(testing::internal::linked_ptr_internal const*)':
/home/user/googletest/googletest/include/gtest/internal/gtest-linked_ptr.h:110: undefined reference to `testing::internal::g_linked_ptr_mutex'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我的 Cmake 文件:

target_link_libraries(Test
  ${Boost_LIBRARIES}
  ${GTEST_LIBRARIES}
  log4cplus
  gtest
  gtest_main
  pthread
  "-L/usr/local/lib/libgmock.a" gmock
  "-L/usr/local/lib/libgmock_main.a" gmock_main
  #  gmock
  #  gmock_main
  proto
  $<TARGET_OBJECTS:logging_dev>)

我尝试使用 gmock 和 gmock_main 并提供硬编码路径。 所有其他仅使用 GTest 的测试都可以成功编译和运行。

我做错了什么?:)

【问题讨论】:

  • GTEST_LIBRARIES 包含什么?该变量是否已填充?此外,this 评论似乎表明您不再需要链接到gmock_main。在提供链接库的完整路径时,您不需要在target_link_libraries() 中添加-L 前缀。
  • GTEST_LIBRARIES 是空的...所以我删除了-L,结果如下:``` /usr/local/lib/libgmock.a(gmock-all.cc. o): 在函数 `testing::Matcher<:__cxx11::basic_string std::char_traits>, std::allocator > const&>::Matcher(std::__cxx11::basic_string , std::allocator > const&)': gmock-all.cc:(.text+0x10e4): 多重定义 ... ``
  • 如果您提供libgmock 库的完整 路径,则无需将其再次与gmock 链接。您可以从 target_link_libraries() 调用中删除 gmock,因为您已经通过提供库的完整路径来链接它。

标签: c++ cmake googletest googlemock


【解决方案1】:

首先你必须找到gtest by

find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS}/include)

然后将 gtest *.libs 链接到您的测试可执行文件

target_link_libraries(${EXE_NAME} PUBLIC GTest::GTest GTest::Main #other libs)

【讨论】:

    猜你喜欢
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 2012-09-14
    • 2021-10-30
    • 2021-09-24
    相关资源
    最近更新 更多