1

这个问题遵循我的先例

我正在用 OpenCV、Torch 和 NumCpp 编写一个 C++ 程序。该程序现在可以编译并且运行良好,但我需要编写单元测试。

我已经按照谷歌的教程在我的项目中构建 GTest 和 GMock,但它失败了。当我不链接 Torch 库时,它可以工作。

链接 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

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

希望有人能帮忙!

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

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

1 回答 1

3

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

此标志位于 中TORCH_CXX_FLAGS,但是 gtest 使用自己的 CMakeLists.txt 和自己的一组标志。您应该手动添加它。最简单的方法可能是add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)在 CMakeLists.txt 顶部附近的某个地方。

于 2020-07-02T11:45:30.190 回答