如何解决GTest和LibTorch联动冲突

How to solve GTest and LibTorch linkage conflict

本文关键字:LibTorch 冲突 GTest 何解决 解决      更新时间:2023-10-16

这个问题遵循了我的先例。

我正在用OpenCV,Torch和NumCpp编写一个C++程序。该程序现在可以编译和工作正常,但我需要编写单元测试。

我按照谷歌的教程在我的项目中构建GTest和GMock,但它失败了。当我不链接火炬库时,这行得通。

链接 GTest + 手电筒时出错:

/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

我想它来自 Libtorch 定义与 Gtest 同名的宏,谷歌提出了一种解决方法来解决此类问题,但我需要找到哪个宏失败了。

希望有人能帮忙!

这是我的CMakeList.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)

和 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      ""
)

PyTorch使用-D_GLIBCXX_USE_CXX11_ABI=0编译标志(这在2020 IMNSHO中应该是刑事犯罪(。您需要使用此标志编译所有代码,包括 gtest。

这个标志是TORCH_CXX_FLAGS的,但是gtest使用自己的CMakeList.txt有自己的标志集。您应该手动添加它。最简单的方法可能是add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)靠近CMakeLists.txt顶部的某个地方。