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!
OpenCV_LIBSvariable actually contains OpenCV library? See that question (and comments to it) about checking this fact.OpenCV_LIBSorOpen_CV_LIBSvariable 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 withmake VERBOSE=1and check exact command line used for linking.