Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't link a project using cmake with OpenCV and LibTorch

I asked a similar question about linking a project with OpenCV a few days ago. I got that working, but now I've hit a very weird problem using CMake and adding LibTorch to the project.

If I only use OpenCV in the project, everything compiles, links, and runs fine.

But if I add Torch to CMakeLists.txt, I get a linker error:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
set(CMAKE_VERBOSE_MAKEFILE ON)
project(torchscriptie)

find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )

find_package(Torch HINTS "/usr/local/libtorch")
message(STATUS "TORCH_LIBRARIES = ${TORCH_LIBRARIES}")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(torchscriptie main.cpp)
target_link_libraries( torchscriptie ${OpenCV_LIBS} )
target_link_libraries(torchscriptie "${TORCH_LIBRARIES}")
set_property(TARGET torchscriptie PROPERTY CXX_STANDARD 14)

This CMakeLists.txt file causes this error:

CMakeFiles/torchscriptie.dir/main.cpp.o: In function `main':
/code/cpp/torchscriptie/main.cpp:18: undefined reference to `cv::imread(std::string const&, int)'
/code/cpp/torchscriptie/main.cpp:24: undefined reference to `cv::namedWindow(std::string const&, int)'
/code/cpp/torchscriptie/main.cpp:25: undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
collect2: error: ld returned 1 exit status

Here's the command:

/usr/bin/c++  -g  -rdynamic CMakeFiles/torchscriptie.dir/main.cpp.o  -o torchscriptie  -Wl,-rpath,/usr/local/lib:/usr/local/libtorch/lib /usr/local/lib/libopencv_gapi.so.4.3.0 /usr/local/lib/libopencv_stitching.so.4.3.0 /usr/local/lib/libopencv_alphamat.so.4.3.0 /usr/local/lib/libopencv_aruco.so.4.3.0 /usr/local/lib/libopencv_bgsegm.so.4.3.0 /usr/local/lib/libopencv_bioinspired.so.4.3.0 /usr/local/lib/libopencv_ccalib.so.4.3.0 /usr/local/lib/libopencv_dnn_objdetect.so.4.3.0 /usr/local/lib/libopencv_dnn_superres.so.4.3.0 /usr/local/lib/libopencv_dpm.so.4.3.0 /usr/local/lib/libopencv_face.so.4.3.0 /usr/local/lib/libopencv_freetype.so.4.3.0 /usr/local/lib/libopencv_fuzzy.so.4.3.0 /usr/local/lib/libopencv_hdf.so.4.3.0 /usr/local/lib/libopencv_hfs.so.4.3.0 /usr/local/lib/libopencv_img_hash.so.4.3.0 /usr/local/lib/libopencv_intensity_transform.so.4.3.0 /usr/local/lib/libopencv_line_descriptor.so.4.3.0 /usr/local/lib/libopencv_quality.so.4.3.0 /usr/local/lib/libopencv_rapid.so.4.3.0 /usr/local/lib/libopencv_reg.so.4.3.0 /usr/local/lib/libopencv_rgbd.so.4.3.0 /usr/local/lib/libopencv_saliency.so.4.3.0 /usr/local/lib/libopencv_stereo.so.4.3.0 /usr/local/lib/libopencv_structured_light.so.4.3.0 /usr/local/lib/libopencv_superres.so.4.3.0 /usr/local/lib/libopencv_surface_matching.so.4.3.0 /usr/local/lib/libopencv_tracking.so.4.3.0 /usr/local/lib/libopencv_videostab.so.4.3.0 /usr/local/lib/libopencv_viz.so.4.3.0 /usr/local/lib/libopencv_xfeatures2d.so.4.3.0 /usr/local/lib/libopencv_xobjdetect.so.4.3.0 /usr/local/lib/libopencv_xphoto.so.4.3.0 /usr/local/libtorch/lib/libtorch.so /usr/local/libtorch/lib/libc10.so /usr/local/lib/libopencv_shape.so.4.3.0 /usr/local/lib/libopencv_highgui.so.4.3.0 /usr/local/lib/libopencv_datasets.so.4.3.0 /usr/local/lib/libopencv_plot.so.4.3.0 /usr/local/lib/libopencv_text.so.4.3.0 /usr/local/lib/libopencv_dnn.so.4.3.0 /usr/local/lib/libopencv_ml.so.4.3.0 /usr/local/lib/libopencv_phase_unwrapping.so.4.3.0 /usr/local/lib/libopencv_optflow.so.4.3.0 /usr/local/lib/libopencv_ximgproc.so.4.3.0 /usr/local/lib/libopencv_video.so.4.3.0 /usr/local/lib/libopencv_videoio.so.4.3.0 /usr/local/lib/libopencv_imgcodecs.so.4.3.0 /usr/local/lib/libopencv_objdetect.so.4.3.0 /usr/local/lib/libopencv_calib3d.so.4.3.0 /usr/local/lib/libopencv_features2d.so.4.3.0 /usr/local/lib/libopencv_flann.so.4.3.0 /usr/local/lib/libopencv_photo.so.4.3.0 /usr/local/lib/libopencv_imgproc.so.4.3.0 /usr/local/lib/libopencv_core.so.4.3.0 -Wl,--no-as-needed,/usr/local/libtorch/lib/libtorch_cpu.so -Wl,--as-needed /usr/local/libtorch/lib/libc10.so -lpthread -Wl,--no-as-needed,/usr/local/libtorch/lib/libtorch.so -Wl,--as-needed 

I've tried a lot of different combination of commands, but I cannot figure our what's wrong. When I echo the TORCH_LIBRARIES variable, it returns:

torch;torch_library;/usr/local/libtorch/lib/libc10.so

If I change the libaries, for example to torch_cpu, I'm able to link OpenCV libraries.

set(TORCH_LIBRARIES torch_cpu)

I have no idea why Torch libraries are causing link errors with OpenCV in the same project. Any suggestions would be great.

like image 799
Tim K Avatar asked Jun 01 '20 01:06

Tim K


1 Answers

Not sure if this fixes your problem, but I had also a few problems like yours. It is working for me with a cmake file including this:

cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(MyProject)
set(CMAKE_CXX_STANDARD 14)
list(APPEND CMAKE_PREFIX_PATH "folderToLibtorch/libtorch/share/cmake")
find_package(Torch REQUIRED)  
find_package(OpenCV REQUIRED)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lm -ldl")
add_executable(MyProject ...some project files...)
target_link_libraries(MyProject ${TORCH_LIBRARIES} ${OpenCV_LIBS})
set_property(TARGET MyProject PROPERTY CXX_STANDARD 14)

So make sure list(APPEND CMAKE_PREFIX_PATH "folderToLibtorch/libtorch/share/cmake") is included, this did the trick for me. Also take care to get the right libtorch version from pytorch maybe you should use the lower link and not the "pre-..." version. Good Luck

Libtorch download

like image 163
user2267367 Avatar answered Oct 23 '22 17:10

user2267367