本章内容:
1.资源下载
2.解压资源
3.libtorch库文件
4..pro文件配置
5.配置源码
6.测试结果
1.资源下载
torch官网: https://pytorch.org/
一键下载:
wget https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.5.1%2Bcpu.zip
2. 解压资源
博主手动解压
3. libtorch内容
CPU版本与GPU版本所包含的文件目录一致,其中include,lib文件夹为qt所需要引用内容
4.在QT的.pro文件中引用
一下为libc10_cuda.so动态库的GPU头文件,若使用到该动态库,需要添加他的头文件库,相关路径库,以及库文件
5. 测试代码
.pro文件
QT -= gui
CONFIG += c++14 console
CONFIG -= app_bundle
DEFINES += QT_DEPRECATED_WARNINGSSOURCES += main.cpp
INCLUDEPATH += /home/wang/cppTorch/libtorch_CPU/include/torch/csrc/api/include \
/home/wang/cppTorch/libtorch_CPU/include
DEPENDPATH += /home/wang/cppTorch/libtorch_CPU/include/torch/csrc/api/include \
/home/wang/cppTorch/libtorch_CPU/include
LIBS += -L/home/wang/cppTorch/libtorch_CPU/lib -lc10 \
-lcaffe2_module_test_dynamic \
-lcaffe2_observers \
-lonnx -lonnx_proto \
-ltorch_cpu -ltorch
main.cpp
#include "torch/script.h"
#include <iostream>int main() {
torch::Tensor tensor = torch::rand({2, 3});
std::cout << tensor << std::endl;
}
6. 测试结果