VScode + libtorch配置

配置选项:基于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
}

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在VSCode配置PHP环境,可以按照以下步骤进行操作: 1. 首先,确保已经安装了VSCode和PHPStudy。 2. 打开VSCode,在左侧导航栏中选择“扩展”,然后搜索并安装名为“PHP Intellisense”的扩展。该扩展提供了代码智能提示和语法检查功能,使得在编辑PHP代码时更加方便。 3. 打开PHPStudy的安装目录,找到php.exe文件的路径。根据引用中的描述,路径可能会类似于"D:\phpstudy_pro\Extensions\php\php5.3.29nts\php.exe"。 4. 右键点击“此电脑”,选择“属性”,然后点击“高级系统设置”。 5. 在弹出的窗口中,选择“环境变量”。 6. 在“系统变量”中找到名为“PATH”的变量,双击打开。 7. 在弹出的窗口中,点击“新建”,然后输入PHPStudy的php.exe所在的文件夹路径,根据引用的描述,应输入"D:\phpstudy_pro\Extensions\php\php5.3.29nts"。 8. 点击“确定”关闭所有窗口。 现在,你已经成功配置VSCode和PHPStudy的环境。你可以在VSCode中编写和运行PHP代码了。 例如,你可以在VSCode中创建一个新的文件,将引用中的九九乘法表代码复制粘贴进去。然后,保存文件并将其命名为"multiplication.php"。 接下来,在VSCode的顶部菜单中选择“终端”,然后选择“新建终端”。这将会打开一个命令行终端。 在终端中,输入"php multiplication.php",然后按下回车键。你将看到九九乘法表的输出结果。 这样,你就成功配置VSCode和PHPStudy的环境,并且可以在VSCode中编写和运行PHP代码了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [vscode+phpstudy配置php环境](https://blog.csdn.net/weixin_52414722/article/details/127931817)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Vscode+phpstudy配置PHP环境,并在服务器中运行。](https://blog.csdn.net/m0_59081230/article/details/127647529)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值