3

I am trying to build a project with libtorch and opencv as dependencies. I am using cmake as my build system due to the fact that it is recommended for both these libraries. I am currently stuck, I am trying to get a minimal program to compile, using both libtorch and opencv.

My program looks like this

#include <opencv2/opencv.hpp> #include <torch/torch.h> void showImage(cv::Mat); at::Tensor imgToTensor(std::string img_path); using namespace cv; using std::cout; using std::endl; int main() { std::string img_path = "./images/01 HEAVENLY STAR080.png"; auto tensor = imgToTensor(img_path); cout << tensor << endl; } at::Tensor imgToTensor(std::string img_path){ Mat origImage; Mat normalizedImage; Mat sizedImage(500, 200, CV_32FC3); origImage = imread(img_path, 1); origImage.convertTo(normalizedImage, CV_32FC3); resize(normalizedImage, sizedImage, sizedImage.size(), 0, 0, INTER_LINEAR); auto input = torch::from_blob(sizedImage.data, {sizedImage.rows, sizedImage.cols, 3}); return input; } void showImage(Mat image){ namedWindow("Display window", WINDOW_AUTOSIZE); imshow("Display window", image); waitKey(0); } 

This is my CMakeLists.txt:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR) project(ConvNet) set(Torch_DIR /usr/local/libtorch/share/cmake/Torch) find_package(OpenCV REQUIRED) find_package(Torch REQUIRED) include_directories( ${OpenCV_INCLUDE_DIRS} ) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}") add_executable(main main.cpp) target_link_libraries(main "${OpenCV_LIBS}" "${TORCH_LIBRARIES}") 

This is the output of cmake, so i know that the libraries are found:

-- The C compiler identification is GNU 9.3.0 -- The CXX compiler identification is GNU 9.3.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc - works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ - works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Found OpenCV: /usr/local (found version "4.3.0") -- Looking for pthread.h -- Looking for pthread.h - found -- Performing Test CMAKE_HAVE_LIBC_PTHREAD -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed -- Looking for pthread_create in pthreads -- Looking for pthread_create in pthreads - not found -- Looking for pthread_create in pthread -- Looking for pthread_create in pthread - found -- Found Threads: TRUE -- Found Torch: /usr/local/libtorch/lib/libtorch.so -- Configuring done -- Generating done -- Build files have been written to: /home/jacob/Documents/KTH/KEX/codeEnvironment/ML_Classification_Toolkit/ML_tool/ConvNet/build 

and this is the error i get:

/usr/bin/ld: CMakeFiles/main.dir/main.cpp.o: in function `imgToTensor(std::string)': main.cpp:(.text+0x8d9): undefined reference to `cv::imread(std::string const&, int)' /usr/bin/ld: CMakeFiles/main.dir/main.cpp.o: in function `showImage(cv::Mat)': main.cpp:(.text+0xbac): undefined reference to `cv::namedWindow(std::string const&, int)' /usr/bin/ld: main.cpp:(.text+0xc0d): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)' collect2: error: ld returned 1 exit status make[2]: *** [CMakeFiles/main.dir/build.make:122: main] Error 1 make[1]: *** [CMakeFiles/Makefile2:96: CMakeFiles/main.dir/all] Error 2 make: *** [Makefile:104: all] Error 2 

Any help would be greatly appreciated!

6
  • 2
    This is my first time using cmake, I have no idea what I'm doing. Thanks a ton for the constructive criticism! Commented Apr 27, 2020 at 11:02
  • Are you sure that OpenCV_LIBS variable actually contains OpenCV library? See that question (and comments to it) about checking this fact. Commented Apr 27, 2020 at 11:06
  • Yes, it does. The same syntax works when im compiling only opencv. Commented Apr 27, 2020 at 11:19
  • Putting message(STATUS "${Open_CV_LIBS}") prints a list of libraries. Commented Apr 27, 2020 at 11:26
  • So it is OpenCV_LIBS or Open_CV_LIBS variable which contains a list of libraries? Your last comment seems to contradict to the code in the question post. Anywhere, you may build the project with make VERBOSE=1 and check exact command line used for linking. Commented Apr 27, 2020 at 11:39

2 Answers 2

6

The version of libtorch I downloaded did not support cxx11 abi, and was therefore not compatible with opencv. Fixed by changing version of libtorch used.

The version I was using was the pre-cxx11 abi from here: https://pytorch.org/get-started/locally/

I switched to the cxx11 abi.

Sign up to request clarification or add additional context in comments.

Comments

2

Don't use that legacy cmake stuff

CMake simplifies a lot the process of building and linking libraries together.

Instead of manually tell the library path and linker options you can just create a dependency of your target with the library.

Following a short snippet on how it should it look like:

cmake_minimum_required(VERSION 3.10 FATAL_ERROR) project(ConvNet) find_package(OpenCV REQUIRED COMPONENTS opencv_highgui) find_package(Torch REQUIRED) add_executable(main main.cpp) target_link_libraries(main PRIVATE opencv_highgui torch) 

Observations

  1. There is no include directory! This is because the include directory of the library is something that the library itself knows!
  2. target_link_libraries even if it seems the same there's a lot of difference here! Because now we're not telling the compiler what to do but linking the target main to the targets opencv_highgui and torch.

opencv_highgui and torch are targets like main. Targets are created using add_library and add_executable.

A target has a public and a private interface. To set the target properties we call the function target_* (for instance target_compile_features(mytarget PUBLIC cxx_std_20) says that the target mytarget will use C++20 features and if someone links to us he will use the same compiler option automatically`

4 Comments

Alright, I updated my cmake file to your suggestion. I still defined Torch_DIR as it wouldnt work otherwise. However, Im at the same error. Are you saying that the problem could be due to compatibility issues between opencv and libtorch?
I will try to compile on my laptop... maybe I made some typo, let me download opencv and torch :)
It might be due to the 2 libraries using different ABI's? Is there anyway for me to check this?
I think the problem is that i downloaded a pre-compiled libtorch library which is compiled with _GLIBCXX_USE_CXX11_ABI=0 flag, this breaks compatibility with opencv. I am going to download it from source and build libtorch myself and see if it works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.