วิธีแก้ไขข้อขัดแย้งในการเชื่อมโยง Gtest และ LibTorch

คำถามนี้เป็นไปตามแบบอย่างของฉัน< /ก>.

ฉันกำลังเขียนโปรแกรม C++ ด้วย OpenCV, Torch และ NumCpp โปรแกรมคอมไพล์และทำงานได้ดีในตอนนี้ แต่ฉันต้องเขียนการทดสอบหน่วย

ฉันได้ติดตามบทแนะนำของ Google เพื่อสร้าง 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      ""
        )

person totok    schedule 02.07.2020    source แหล่งที่มา
comment
มีเพียงสามมาโครที่คุณสามารถยกเลิกการกำหนดได้ด้วยวิธีแก้ไขปัญหาชั่วคราวนี้: FAIL, SUCCEED และ TEST ฉันสงสัยว่าสิ่งเหล่านี้จะช่วยได้ คุณจะเรียกใช้ตัวเชื่อมโยงได้อย่างไร? คุณสามารถแสดงคำสั่งแบบเต็มได้หรือไม่?   -  person n. 1.8e9-where's-my-share m.    schedule 02.07.2020
comment
@n.'สรรพนาม'm. ฉันได้แก้ไขโพสต์ด้วย CMakeLists ของฉันแล้ว   -  person totok    schedule 02.07.2020
comment
@n.'สรรพนาม'm. คุณแน่ใจหรือว่าเราสามารถกำหนดคำเหล่านี้ใหม่ได้เท่านั้น ฉันเพิ่ม -DGTEST_DONT_DEFINE_ASSERT_EQ=1 ลงในตัวเลือกการคอมไพล์ และต้องเปลี่ยนชื่อ ASSERT_EQ ในโค้ดทดสอบเป็น GTEST_ASSERT_EQ เพื่อพิสูจน์ว่าสิ่งนี้อาจใช้ได้   -  person totok    schedule 02.07.2020
comment
CMakeLists ถูกเอาออกจากการดำเนินการมากเกินไป ฉันไม่สามารถบอกได้ว่ามันทำอะไรโดยไม่ต้องสร้างโครงการของคุณจริงๆ นี่คือเหตุผลที่ฉันขอคำสั่งการเรียกใช้ลิงเกอร์ แทนที่จะขอสคริปต์การกำหนดค่าที่สร้างมันขึ้นมา   -  person n. 1.8e9-where's-my-share m.    schedule 02.07.2020
comment
ฉันจะให้สิ่งที่คุณต้องการได้อย่างไร?   -  person totok    schedule 02.07.2020
comment
เห็นได้ชัดว่า ASSERT_ ทั้งหมด?? มาโครยังสามารถไม่สามารถกำหนดได้แม้ว่าจะไม่ได้จัดทำเป็นเอกสารก็ตาม   -  person n. 1.8e9-where's-my-share m.    schedule 02.07.2020
comment
ลอง make VERBOSE=1 ซึ่งจะพิมพ์คำสั่งเมื่อมีการเรียกใช้   -  person n. 1.8e9-where's-my-share m.    schedule 02.07.2020
comment
ที่นี่คือเอาต์พุตแบบละเอียด   -  person totok    schedule 02.07.2020
comment
มันแปลก ๆ. คำสั่งดูโอเค ฟังก์ชันที่เป็นปัญหาควรจะอยู่ใน libgtest.a ซึ่งสร้างจาก gtest-all.cc.o ซึ่งสร้างจาก gtest-all.cc ซึ่งรวมถึง gtest.cc ซึ่งมีฟังก์ชันที่เป็นปัญหา ลองใช้ nm -C libgtest.a และ nm -C gtest-all.cc.o และ grep สำหรับ EqFailure   -  person n. 1.8e9-where's-my-share m.    schedule 02.07.2020
comment
ให้เราสนทนาต่อในการแชท   -  person totok    schedule 02.07.2020


คำตอบ (1)


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

person n. 1.8e9-where's-my-share m.    schedule 02.07.2020
comment
คุณ. เป็น. ก. อัจฉริยะ. ขอบคุณมากสำหรับความช่วยเหลือ! คุณช่วยแก้ไขคำตอบได้ไหม? -D ต้องไม่อยู่ในคำสั่ง add_compile_definitions เพราะจะถูกเพิ่มโดยอัตโนมัติ ขอบคุณสำหรับเวลาที่คุณสละเวลาเพื่อฉัน! - person totok; 02.07.2020
comment
คุณช่วยอธิบายฉันหรือส่งเอกสารเกี่ยวกับสาเหตุที่ห้ามใช้ธง ABI นี้ให้ฉันได้ไหม - person totok; 02.07.2020
comment
การตั้งค่าสถานะจะดาวน์เกรด ABI ไลบรารีมาตรฐานเป็นเวอร์ชันก่อน gcc5.1 ซึ่งเป็นเวอร์ชันเก่าและทำให้รองรับ c++11 เต็มรูปแบบไม่ได้ (นี่คือเหตุผลในการเปลี่ยน ABI ในตอนแรก) การเปลี่ยนแปลงนี้เข้ากันไม่ได้แบบย้อนหลัง ดังนั้นจึงเป็นไปไม่ได้ที่จะเชื่อมโยงไลบรารีที่ใช้ ABI เก่าและใหม่เข้าด้วยกัน ค้นหา dual abi สำหรับข้อมูลเพิ่มเติม - person n. 1.8e9-where's-my-share m.; 02.07.2020