How to solve GTest and LibTorch linkage conflict

爷,独闯天下 提交于 2021-01-29 09:32:55

问题


This question follows my precedent one.

I'm writing a C++ program with OpenCV, Torch and NumCpp. The program compiles and works fine for now, but I need to write unit tests.

I've followed google's tutorial to build GTest and GMock inside my project, but it fails. When I don't link Torch libraries, that works.

Error when linking GTest + Torch :

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

I suppose it comes from Libtorch defining macros with the same name as Gtest, and Google proposes a workaround to fix this type of problem, but I need to find which macro is failing.

Hope someone can help !

Here is my CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
project(TryGtest)

set(CMAKE_CXX_STANDARD 14) # C14 required to compile Torch
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)

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
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()
# -------------------------------------------------------------------------
enable_testing()
include_directories("${gtest_SOURCE_DIR}/include")


# Program executable
add_executable(TryGTest src/main.cpp src/box.cpp include/util.h)

# Test executable
add_executable(TryGTest_test test/main.cpp src/box.cpp test/boxTest.cpp include/util.h)

target_link_libraries(TryGTest PRIVATE pthread dl util ${TORCH_LIBRARIES} ${OpenCV_LIBS} )
target_link_libraries (TryGTest_test PRIVATE pthread dl util ${TORCH_LIBRARIES} ${OpenCV_LIBS} gtest gmock)

and the CMakeLists.txt.in

cmake_minimum_required(VERSION 2.8.2)

project(googletest-download NONE)

include(ExternalProject)
ExternalProject_Add(googletest
        GIT_REPOSITORY    https://github.com/google/googletest.git
        GIT_TAG           release-1.10.0
        SOURCE_DIR        "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
        BINARY_DIR        "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
        CONFIGURE_COMMAND ""
        BUILD_COMMAND     ""
        INSTALL_COMMAND   ""
        TEST_COMMAND      ""
        )

回答1:


PyTorch uses -D_GLIBCXX_USE_CXX11_ABI=0 compilation flag (which should be a criminal offense in 2020 IMNSHO). You need to compile all of the code, including gtest, with this flag.

This flag is in TORCH_CXX_FLAGS, however gtest uses its own CMakeLists.txt with its own set of flags. You should add it manually. The easiest way is probably with add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0) somewhere near the top of CMakeLists.txt.



来源:https://stackoverflow.com/questions/62693218/how-to-solve-gtest-and-libtorch-linkage-conflict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!