คำถามนี้เป็นไปตามแบบอย่างของฉัน< /ก>.
ฉันกำลังเขียนโปรแกรม 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 ""
)
-DGTEST_DONT_DEFINE_ASSERT_EQ=1
ลงในตัวเลือกการคอมไพล์ และต้องเปลี่ยนชื่อ ASSERT_EQ ในโค้ดทดสอบเป็น GTEST_ASSERT_EQ เพื่อพิสูจน์ว่าสิ่งนี้อาจใช้ได้ - person totok   schedule 02.07.2020make VERBOSE=1
ซึ่งจะพิมพ์คำสั่งเมื่อมีการเรียกใช้ - person n. 1.8e9-where's-my-share m.   schedule 02.07.2020nm -C libgtest.a
และnm -C gtest-all.cc.o
และ grep สำหรับEqFailure
- person n. 1.8e9-where's-my-share m.   schedule 02.07.2020