CMAKELists
| cmake_minimum_required(VERSION 3.0 FATAL_ERROR) |
| project(example) |
| |
| set(CMAKE_PREFIX_PATH /home/xuliangyu/libtorch) |
| set(Torch_DIR /home/shiyanshi/libtorch/share/cmake/) |
| # 修改这里为自己的libtorch cmake路径,也可以用pytorch安装时编译的pkg里的cmake |
| find_package(Torch REQUIRED) |
| #find_package(CUDA REQUIRED) |
| |
| INCLUDE_DIRECTORIES( |
| "/home/shiyanshi/libtorch/include" |
| "/home/shiyanshi/liborch/include/csrc/api/include" |
| ) |
| |
| add_executable(example example.cpp) |
| target_link_libraries(example "${TORCH_LIBRARIES}") |
| set_property(TARGET example PROPERTY CXX_STANDARD 14) |
| |
Tasks.json
| { |
| "version": "2.0.0", |
| "tasks": [ |
| { |
| "type": "cppbuild", |
| "label": "cmake build", |
| "command": "cmake", |
| "args": [ |
| "-D", |
| "CMAKE_PREFIX_PATH=/home/shiyanshi/libtorch/share/cmake", |
| "..", |
| "&&", |
| "make", |
| "-j" |
| ], |
| "options": { |
| "cwd": "/home/shiyanshi/Cuda_Lib/cuda_2/build" |
| }, |
| "problemMatcher": [ |
| "$gcc" |
| ], |
| "group": { |
| "kind": "build", |
| "isDefault": true |
| }, |
| "detail": "compiler: /usr/bin/g++" |
| } |
| ] |
| } |
Launch.json
| { |
| |
| |
| |
| "version": "0.2.0", |
| "configurations": [ |
| { |
| "name": "(gdb) Launch", |
| "type": "cppdbg", |
| "request": "launch", |
| "program": "${workspaceFolder}/build/example", |
| "args": [], |
| "stopAtEntry": true, |
| "cwd": "${workspaceFolder}", |
| "environment": [], |
| "externalConsole": false, |
| "MIMode": "gdb", |
| "setupCommands": [ |
| { |
| "description": "Enable pretty-printing for gdb", |
| "text": "-enable-pretty-printing", |
| "ignoreFailures": true |
| }, |
| { |
| "description": "Set Disassembly Flavor to Intel", |
| "text": "-gdb-set disassembly-flavor intel", |
| "ignoreFailures": true |
| } |
| ], |
| "preLaunchTask": "cmake build", |
| "miDebuggerArgs": "/usr/bin/gdb" |
| } |
| ] |
| } |
| |
测试C++文件
| #include <torch/script.h> |
| #include <iostream> |
| |
| int main() { |
| torch::Tensor foo = torch::arange(25).reshape({5, 5}); |
| torch::Tensor bar = torch::einsum("ii", foo); |
| |
| std::cout << "==> matrix is:\n " << foo << std::endl; |
| std::cout << "==> trace of it is:\n " << bar << std::endl; |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 提示词工程——AI应用必不可少的技术
· 地球OL攻略 —— 某应届生求职总结
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界