Troubles while compiling C++ program with PyTorch, HElib and OpenCV
I'm trying to compile my C++ program that uses the libraries HElib, OpenCV and PyTorch. I'm on Ubuntu 20.04. The entire code is:
#include &lt;iostream&gt;
#include &lt;fstream&gt;

#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;cstdint&gt;
#include &lt;memory&gt;
#include &lt;stdio.h&gt;
#include &lt;opencv2/core.hpp&gt;
#include &lt;opencv2/imgcodecs.hpp&gt;
#include &lt;opencv2/highgui.hpp&gt;
#include &lt;helib/helib.h&gt;
#include &lt;torch/torch.h&gt;
#include &quot;include/mnist/mnist_reader.hpp&quot;

using namespace cv;
using namespace torch;
using namespace std;
using namespace mnist;
using namespace helib;

int main(int argc, char* argv[]) {
  Tensor tensor = torch::rand({2, 3});
  cout &lt;&lt; tensor &lt;&lt; endl;

  Mat imageMat = Mat(image, true).reshape(0, 1).t();
  return 0;
}

(where image is a 28x28 matrix).
I'm compiling it with the command (I know I should be using cmake but I'm new to C++ and for now I'd like to learn how to link libraries properly just from the command line):
g++ -g -O2 -std=c++17 -pthread -march=native prova.cpp -lopencv_core -lopencv_highgui -lopencv_imgcodecs -o prova -I/home/lulu/helib_install/helib_pack/include -I/usr/include/opencv4 -I/home/lulu/libtorch/include -I/home/lulu/libtorch/include/torch/csrc/api/include -I/home/lulu/libtorch/include/torch -L/home/lulu/helib_install/helib_pack/lib -L/usr/include/opencv4 -L/home/lulu/libtorch/lib -lhelib -lntl -lgmp -lm -ltorch -ltorch_cpu -lc10 -D_GLIBCXX_USE_CXX11_ABI=0

The error I get is the following:
/usr/bin/ld: /tmp/cc3mP2mc.o: in function `cv::Mat::Mat(int, int, int, void*, unsigned long)':
/usr/include/opencv4/opencv2/core/mat.inl.hpp:548: undefined reference to `cv::error(int, std::string const&amp;, char const*, char const*, int)'
collect2: error: ld returned 1 exit status

Deleting the flag -D_GLIBCXX_USE_CXX11_ABI=0 doesn't help, I tried.
I also tried setting the variable LD_LIBRARY_PATH to /home/lulu/libtorch/lib, but neither that helps.
I think I'm linking all the libraries I need, what am I missing?
Thanks in advance for the help.

I found the answer, but I can't really explain with my little experience what I've done, I'll just illustrate the passages.
I've re-downloaded PyTorch from its website, selecting the libtorch-cxx11-abi-shared-with-deps version (the one compiled with -D_GLIBCXX_USE_CXX11_ABI=1).
Then I had to add to the compilation command the flag -Wl,-rpath,/path/to/pytorch/lib, because for some reason the compiler didn't find libc10 and libtorch_cpu, so the final command was:
g++ -g -O2 -std=c++17 \
-pthread \
-march=native \ 
-I/home/lulu/helib_install/helib_pack/include \ 
-I/usr/include/opencv4 \
-I/home/lulu/libtorch/include \ 
-I/home/lulu/libtorch/include/torch/csrc/api/include \ 
-/home/lulu/libtorch/include/torch \
-L/home/lulu/helib_install/helib_pack/lib \ 
-L/usr/include/opencv4 \
-L/home/lulu/libtorch/lib \ 
-Wl,-rpath,/home/lulu/libtorch/lib \ 
prova.cpp \
-lopencv_core -lopencv_highgui -lopencv_imgcodecs \ 
-lhelib -lntl -lgmp -lm \
-ltorch -ltorch_cpu -lc10 \
-o prova

