CMake:无法与GMock链接

2022-08-26 00:00:00 cmake c++ googletest googlemock

我正尝试在单元测试中使用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 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)

相关文章