Reputation: 103
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!
Upvotes: 3
Views: 3542
Reputation: 103
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.
Upvotes: 6
Reputation: 7368
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
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`
Upvotes: 2