配置选项:基于Linux,VScode + libtorch
步骤一:libtorch下载(DEBUG版本)
前往libtorch官网https://pytorch.org/get-started/locally/,选择对应版本的libtorch进行下载
解压完成后,放入D盘或者其他位置,记录其路径即可。
步骤二:安装VScode(这边不再赘述),安装完成后,从官网下载cmake进行安装,这边不推荐通过VScode自带的Extensions增加cmake和cmake tools模块。
步骤三:安装好VScode后,新建项目,然后新建main.cpp,然后逐个配置CMakeLists.txt,launch.json,tasks.json,c_cpp_properties.json文件。
main.cpp 代码如下:
#include<torch/script.h>
#include<torch/torch.h>
using namespace std;
int main()
{
torch::Tensor output;
cout << "cuda is_available: " << torch::cuda::is_available() << endl;
torch::DeviceType device = at::kCPU;
if (torch::cuda::is_available())
device = at::kCUDA;
output = torch::randn({3,3}).to(device);
cout << output << endl;
return 0;
}
CMakeLists.txt配置如下:
cmake_minimum_required(VERSION 3.29.0 FATAL_ERROR)
project(main VERSION 0.1.0)
set(Torch_DIR "[你的libtorch目录]/include/torch")
find_package(Torch REQUIRED)
message(STATUS "torch path: ${Torch_DIR}")
add_executable(main main.cpp)
link_directories(${TORCH_LIBRARIES})
set(TORCH_LIBRARIES "[你的libtorch目录]/include/torch/**" "[你的libtorch目录]/include/torch/csrc/api/include/torch/**")
target_link_libraries(main ${TORCH_LIBRARIES})
set_property(TARGET PROJECT1 PROPERTY CXX_STANDARD 17)
launch.json 代码如下:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - 生成和调试活动文件",
"type": "cppdbg", //
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [], //往可执行文件传参
"stopAtEntry": false,
"cwd": "${fileDirname}", //进入当前目录
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files (x86)\\mingw64\\bin\\gdb.exe", //调试器路径
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe 生成活动文件"
//在调试之前要做的工作,生成可执行文件
//对应于tasks.json的label
}
]
}
tasks.json配置如下:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files (x86)\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: C:\\Program Files (x86)\\mingw64\\bin\\g++.exe"
}
],
"version": "2.0.0"
}
需要在c_cpp_properties.json中配置除workspaceFolder外的其他链接地址,"cStandard": "c17",选择C++ 17语言标准:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"[你的libtorch地址]/include/torch/**",
"[你的libtorch地址]/include/torch/csrc/api/include/torch/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\Program Files (x86)\\mingw64\\bin\\gcc.exe",
"cStandard": "c17",
"cppStandard": "gnu++14",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}