简体   繁体   English

使用 PyTorch、HElib 和 OpenCV 编译 C++ 程序时出现问题

[英]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 .我正在尝试编译我的 C++ 程序,该程序使用库HElibOpenCVPyTorch I'm on Ubuntu 20.04.我在 Ubuntu 20.04。 The entire code is:整个代码是:

#include <iostream>
#include <fstream>

#include <string>
#include <vector>
#include <cstdint>
#include <memory>
#include <stdio.h>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <helib/helib.h>
#include <torch/torch.h>
#include "include/mnist/mnist_reader.hpp"

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 << tensor << endl;

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

(where image is a 28x28 matrix). (其中图像是 28x28 矩阵)。

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):我正在用命令编译它(我知道我应该使用 cmake 但我是 C++ 的新手,现在我想学习如何从命令行正确链接库):

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&, 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.删除标志 -D_GLIBCXX_USE_CXX11_ABI=0 没有帮助,我试过了。

I also tried setting the variable LD_LIBRARY_PATH to /home/lulu/libtorch/lib, but neither that helps.我还尝试将变量 LD_LIBRARY_PATH 设置为 /home/lulu/libtorch/lib,但这都无济于事。

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).我从它的网站重新下载了 PyTorch,选择了libtorch-cxx11-abi-shared-with-deps版本(使用 -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:然后我不得不在编译命令中添加标志 -Wl,-rpath,/path/to/pytorch/lib,因为出于某种原因编译器没有找到 libc10 和 libtorch_cpu,所以最后的命令是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM