'LibTorch with OpenCV: version GOMP_5.0 not found

I'm trying to use OpenCV and LibTorch in the same project. Libtorch is installed in /usr/include/libtorch, downloaded from the PyTorch website. I'm using the cxx11 ABI version for CUDA 11.3.

Here's my CMakeLists.txt file:

cmake_minimum_required(VERSION 3.23 FATAL_ERROR)
project(chess-rl VERSION 1.0)

find_package( OpenCV REQUIRED )

set(CMAKE_PREFIX_PATH /usr/include/libtorch/share/cmake/)

find_package(Torch REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(${OpenCV_LIBS})
include_directories(${TORCH_INCLUDE_DIRS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

file (GLOB SOURCE_FILES 
  ${PROJECT_SOURCE_DIR}/src/*.cc
  ${PROJECT_SOURCE_DIR}/src/chess/*.cc
)

add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} "${TORCH_LIBRARIES}" )
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 20)

The program has a #include <opencv2/opencv.hpp> line at the top. Compilation works fine, but running the executable gives me this error:

/usr/include/libtorch/lib/libgomp-52f2fd74.so.1: version `GOMP_5.0' not found (required by /usr/lib/libvtkCommonCore.so.1)

I believe libtorch is trying to use a library that is incompatible with OpenCV. If I run the program with LD_PRELOAD=/usr/lib/libgomp.so ./build/my-program, it runs fine.

How can I fix this error without having to use that environment variable? Is there a way to link that particular library in CMake?



Solution 1:[1]

I faced the same problem, and your thread contributed to one solution. You need to add a shared library in your CMakelists.txt file.

add_library(libName SHARED IMPORTED)
set_property(TARGET libName PROPERTY IMPORTED_LOCATION "/usr/lib/libgomp.so")
target_link_libraries(PROJECT_NAME
libName)

I hope I could help you With kind regards

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Mueller