如何在CMake中使用GTest?遵循Google指南时的链接问题 [英] How to use GTest with CMake ? Linkage problem when following Google's guide

查看:457
本文介绍了如何在CMake中使用GTest?遵循Google指南时的链接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I started to use boost test for my project, but I need to mock static methods, so I try to switch to GTest and GMock.

我遵循了非常清晰的指南以及Google的CMakeLists似乎正在完成其工作:

I followed the really clear guide from google, and the CMakeLists seems to be doing its job :

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 目标时,出现以下错误

But when I'm compiling the POC_V4_tests target, I've got the following error

/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 ++的新手,所以我可能错过了一些非常简单的东西。有人可以帮我吗?

I'm new to C++, so I may have missed something really simple. Can someone help me ?

编辑:

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

I created an empty project, and it's working properly. I added the different dependencies, and found out that the problem comes from libtorch !

libtorch可能定义了一些名称与GTest相同的宏。我还没有找到,但是我希望我可以使用 Google教程的最后一部分。如果有人想找到哪个宏失败了,那将对我有很大帮助! :D

It is possible that libtorch defines some macros with the same names than GTest. I didn't find which one yet, but I hope I will be able to fix it with the last part of google's tutorial. If someone has an idea to find which macro is failing, it would help me a lot ! :D

感谢所有现在试图帮助我的人,希望您能继续努力,直到我们纠正此问题!

Thanks to all people who tried to help me for now, I hope you will continue until we correct this !

推荐答案

我发现问题不是出自GTest,而是出自libtorch,后者是用CXX_ABI符号编译的。只需在CMakeList顶部添加 add_compile_definitions(_GLIBCXX_USE_CXX11_ABI = 0)即可。

I found out that the problem was not from GTest, but from libtorch, which is compiled with the CXX_ABI symbol. Just need to add add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0) on top of the CMakeLists and that will work.

阅读此问题,并提供详细的答案。

Read this question with a detailed answer.

这篇关于如何在CMake中使用GTest?遵循Google指南时的链接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆