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

标签 c++ ubuntu gcc pytorch

我正在尝试编译使用库 HElib 的 C++ 程序, OpenCVPyTorch .我在 Ubuntu 20.04 上。整个代码是:

#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;
}
(其中图像是 28x28 矩阵)。
我正在使用命令编译它(我知道我应该使用 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
我得到的错误如下:
/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
删除标志 -D_GLIBCXX_USE_CXX11_ABI=0 没有帮助,我试过了。
我还尝试将变量 LD_LIBRARY_PATH 设置为/home/lulu/libtorch/lib,但这都没有帮助。
我想我正在链接我需要的所有库,我错过了什么?
在此先感谢您的帮助。

最佳答案

我找到了答案,但我不能用我的小经验来解释我做了什么,我只会说明这些段落。
我已经从它的 website 重新下载了 PyTorch ,选择 libtorch-cxx11-abi-shared-with-deps 版本(使用 -D_GLIBCXX_USE_CXX11_ABI=1 编译的版本)。
然后我不得不在编译命令中添加标志 -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

关于c++ - 使用 PyTorch、HElib 和 OpenCV 编译 C++ 程序时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72030738/

相关文章:

ubuntu - 入口点脚本 : "no such file or directory" 上的 Docker 错误

c - 如何管理模块特定的指针

c++ - VC++ 与 GCC 预处理器

c# - 根据以下标准开发桌面应用程序的语言/平台

c++ - 个别实例有效,但数组显示内存损坏

grails - 如何在 Linux Ubuntu LTS 上安装 Grails?

ubuntu - Ubuntu 上的 AWS + Remmina - Remmina 进入循环尝试重新连接

c++ - 为什么 RegSetValueEx 即使在我违反有关计算长度中 NUL 终止的规则时仍然有效?

c++ - 将高斯模糊应用于单个像素

适用于所有 x64 平台的 gcc 通用行军