c++ - 如何在CMake中使用GTest?遵循Google指南时的链接问题

标签 c++ cmake linker googletest

我开始对项目使用boost测试,但是我需要模拟静态方法,因此我尝试切换到GTest和GMock。
我遵循了谷歌非常清晰的guide,而CMakeLists似乎正在发挥作用:
CMakeLists.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相同的宏。我还没有找到哪一个,但是我希望我可以用last part of google's tutorial修复它。如果有人想找到哪个宏失败了,那将对我有很大帮助! :D
感谢所有现在试图帮助我的人,我希望您能继续努力直到我们纠正此问题!

最佳答案

我发现问题不是出自GTest,而是出自libtorch,后者是用CXX_ABI符号编译的。只需在CMakeLists顶部添加add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)即可。
阅读this question,并提供详细答案。

关于c++ - 如何在CMake中使用GTest?遵循Google指南时的链接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62654297/

相关文章:

c++ - Visual Studio中静态库项目之间 Unresolved external 问题

c++ - 如何获取 OCaml 链接器标志以与 C++ cmake 构建链接

c++ - C++如何从数组中获取最长的素数序列

c++ - 在文本文件中搜索短语 C++

c++ - 模板函数不同的结果取决于引用类型

cmake - 创建调用子项目自定义目标的自定义目标

linux - Mac OS X 10.9,安装 cmake 时无法在此系统上找到合适的 C 编译器

c++ - CMake:当输入列表的头文件部分更改与正在输出的对象无关时,是否可以重建目标?

c++ - glVertexAttribPointer 步幅参数如何工作?

java - 从 jar 导入 java 库类时,这是否被视为静态链接?还是动态的?