如何将GTest与CMake一起使用?遵循谷歌指南时的链接问题

How to use GTest with CMake ? Linkage problem when following Google's guide

本文关键字:谷歌 问题 链接 GTest CMake 一起      更新时间:2023-10-16

我开始在我的项目中使用boost测试,但我需要模拟静态方法,所以我尝试切换到GTest和GMock。

我遵循了谷歌非常清晰的指南,CMakeLists似乎正在做它的工作:

CMakeList.txt

cmake_minimum_required(VERSION 3.15)
project(POC_V4)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# Specifying we are using pthread for UNIX systems.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS} -pthread -Wall")
find_package(OpenCV REQUIRED)
find_package(Torch REQUIRED)
# Package needed for Boost tests
find_package (Boost REQUIRED COMPONENTS unit_test_framework)
include_directories (${Boost_INCLUDE_DIRS})
if(NOT Torch_FOUND)
message(FATAL_ERROR "Pytorch Not Found!")
endif(NOT Torch_FOUND)
message(STATUS "Pytorch status :")
message(STATUS "    libraries: ${TORCH_LIBRARIES}")
message(STATUS "    Torch Flags: ${TORCH_CXX_FLAGS}")
message(STATUS "OpenCV library status :")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

# -------- GOOGLE TEST ----------
# Download and unpack googletest at configure time
enable_testing()
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()
# -------------------------------------------------------------------------
# Program executable
add_executable(POC_V4 src/main.cpp <all my other files>)
# Test executable
add_executable(POC_V4_tests test/main.cpp <all my other files>)
target_link_libraries(POC_V4 pthread dl util ${TORCH_LIBRARIES} ${OpenCV_LIBS} )
target_link_libraries (POC_V4_tests gtest pthread dl util ${TORCH_LIBRARIES} ${OpenCV_LIBS} )

输出

-- Pytorch status :
--     libraries: torch;torch_library;/usr/lib/libc10.so
--     Torch Flags: -D_GLIBCXX_USE_CXX11_ABI=0
-- OpenCV library status :
--     version: 4.2.0
--     libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio
--     include path: /usr/local/include/opencv4
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/tmp.1U2MhnhFdi/cmake-build-debug-<projectName>_ubuntu/googletest-download
[ 11%] Performing update step for 'googletest'
Current branch master is up to date.
[ 22%] No configure step for 'googletest'
[ 33%] No build step for 'googletest'
[ 44%] No install step for 'googletest'
[ 55%] No test step for 'googletest'
[ 66%] Completed 'googletest'
[100%] Built target googletest
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/tmp.1U2MhnhFdi/cmake-build-debug-<projectName>_ubuntu

但是当我编译POC_V4_tests目标时,我遇到了以下错误

/usr/bin/ld: CMakeFiles/POC_V4_tests.dir/test/boxTest.cpp.o: in function `testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&)':
/tmp/tmp.1U2MhnhFdi/cmake-build-debug-<projectName>_ubuntu/googletest-src/googletest/include/gtest/gtest.h:1528: undefined reference to `testing::internal::EqFailure(char const*, char const*, std::string const&, std::string const&, bool)'
collect2: error: ld returned 1 exit status

我是C++新手,所以我可能错过了一些非常简单的东西。有人可以帮助我吗?

编辑:

我创建了一个空项目,它工作正常。我添加了不同的依赖项,发现问题来自 libtorch!

libtorch 可能定义了一些与 GTest 同名的宏。我还没有找到哪一个,但我希望我能够用谷歌教程的最后一部分修复它。如果有人有找到哪个宏失败的想法,这将对我有很大帮助!:D

感谢所有试图帮助我的人,我希望你能继续,直到我们纠正这一点!

我发现问题不是来自GTest,而是来自使用CXX_ABI符号编译的libtorch。只需要在CMakeList上添加add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)就可以了。

阅读这个问题并附上详细的答案。