CMake:无法与GMock链接链接、CMake、GMock

2023-09-03 13:23:45 作者:称心

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

我尝试了使用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)