ubuntu+vscode+cmake 安装libtorch

2023-12-15 13:33:46

安装流程

1、下载libtorch
官方地址:https://pytorch.org/
首先在官网下载,或者用指令下载:下载自己要的对应版本
cd进入你的目录,下载到当前目录

wget https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.7.1%2Bcpu.zip

2、解压

unzip libtorch-cxx11-abi-shared-with-deps-1.7.1+cpu

3、创建项目文件夹,把libtorch放到项目目录下,文件夹下目录如下

.
├── CMakeLists.txt
└── main.cpp
└── libtorch

按照下面修改CMakeLists.txt

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(test-libtorch)

#1
#set(Torch_DIR /kwj/aot/cpp_demo/libtorch/share/cmake/Torch) #例如
set(Torch_DIR ~/libtorch/share/cmake/Torch)  #~是解压的libtorch的绝对路径

#2
find_package(Torch REQUIRED)

#3
set(CMAKE_CXX_FLAGS "${CAMKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

#main.cpp exe
add_executable(test-libtorch main.cpp)

#4 link libtorch .a .so
target_link_libraries(test-libtorch "${TORCH_LIBRARIES}")

#5
set_property(TARGET test-libtorch PROPERTY CXX_STANDARD 14)

main.cpp

#include<torch/torch.h>
#include<iostream>
//using namespace std;
int main(){
    torch::Tensor tensor = torch::eye(3);
    std::cout << tensor << std::endl;
}

编译

mkdir build
cd build
cmake ..
make
./test-libtorch

文章来源:https://blog.csdn.net/qq_41643701/article/details/134694600
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。