I’m trying to deploy model by C++, I got this error when <torch/script.h> is included in my code:
mat_tensor.cpp:(.text+0x6d): undefined reference to `cv::imread(std::string const&, int)'
mat_tensor.cpp:(.text+0xad): undefined reference to `cv::namedWindow(std::string const&, int)'
mat_tensor.cpp:(.text+0x126): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/mat_tensor.dir/build.make:116: recipe for target 'mat_tensor' failed
make[2]: *** [mat_tensor] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/mat_tensor.dir/all' failed
make[1]: *** [CMakeFiles/mat_tensor.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

When I removed torch from my code and CMakeLists.txt, everything words well.
My code:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <torch/script.h>
#include <string>

int main()
{
    std::string p = "/home/dai/scripts/pytorch_chineseocr/pictures/1.png";
    cv::Mat img = cv::imread(p);
    cv::namedWindow("img");
    cv::imshow("img",img);
    cv::waitKey(0);
    return 0;
}

CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(mat_tensor)

SET(OpenCV_DIR /home/dai/library/opencv-2.4.13.6/build)
SET(Torch_DIR /home/dai/library/libtorch_cpu/share/cmake/Torch)
find_package(OpenCV REQUIRED)
find_package(Torch REQUIRED)
add_executable(mat_tensor mat_tensor.cpp)
target_link_libraries(mat_tensor ${OpenCV_LIBRARIES})
target_link_libraries(mat_tensor ${TORCH_LIBRARIES})

My Libtorch version is libtorch1.3+cpu, and OpenCV version is 2.4.13( 2.4.9 doesn’t work either).