I started to use boost test for my project, but I need to mock static methods, so I try to switch to GTest and GMock.
I followed the really clear guide from google, and the CMakeLists seems to be doing its job :
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(POC_V4)
set(CMAKE_CXX_STANDARD 14)
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)
# Package needed for Boost tests
find_package (Boost REQUIRED COMPONENTS unit_test_framework)
include_directories (${Boost_INCLUDE_DIRS})
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
enable_testing()
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()
# -------------------------------------------------------------------------
# Program executable
add_executable(POC_V4 src/main.cpp <all my other files>)
# Test executable
add_executable(POC_V4_tests test/main.cpp <all my other files>)
target_link_libraries(POC_V4 pthread dl util ${TORCH_LIBRARIES} ${OpenCV_LIBS} )
target_link_libraries (POC_V4_tests gtest pthread dl util ${TORCH_LIBRARIES} ${OpenCV_LIBS} )
Output
-- Pytorch status :
-- libraries: torch;torch_library;/usr/lib/libc10.so
-- Torch Flags: -D_GLIBCXX_USE_CXX11_ABI=0
-- OpenCV library status :
-- version: 4.2.0
-- libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio
-- include path: /usr/local/include/opencv4
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/tmp.1U2MhnhFdi/cmake-build-debug-<projectName>_ubuntu/googletest-download
[ 11%] Performing update step for 'googletest'
Current branch master is up to date.
[ 22%] No configure step for 'googletest'
[ 33%] No build step for 'googletest'
[ 44%] No install step for 'googletest'
[ 55%] No test step for 'googletest'
[ 66%] Completed 'googletest'
[100%] Built target googletest
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/tmp.1U2MhnhFdi/cmake-build-debug-<projectName>_ubuntu
But when I'm compiling the POC_V4_tests
target, I've got the following error
/usr/bin/ld: CMakeFiles/POC_V4_tests.dir/test/boxTest.cpp.o: in function `testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&)':
/tmp/tmp.1U2MhnhFdi/cmake-build-debug-<projectName>_ubuntu/googletest-src/googletest/include/gtest/gtest.h:1528: 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'm new to C++, so I may have missed something really simple. Can someone help me ?
EDIT:
I created an empty project, and it's working properly. I added the different dependencies, and found out that the problem comes from libtorch !
It is possible that libtorch defines some macros with the same names than GTest. I didn't find which one yet, but I hope I will be able to fix it with the last part of google's tutorial. If someone has an idea to find which macro is failing, it would help me a lot ! :D
Thanks to all people who tried to help me for now, I hope you will continue until we correct this !
I found out that the problem was not from GTest, but from libtorch, which is compiled with the CXX_ABI symbol. Just need to add add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)
on top of the CMakeLists and that will work.
Read this question with a detailed answer.