配置VScode开发环境-CUDA编程

如果觉得本篇文章对您的学习起到帮助作用,请 点赞 + 关注 + 评论 ,留下您的足迹💪💪💪

本文主要介绍VScode下的CUDA编程配置,因此记录以备日后查看,同时,如果能够帮助到更多人,也不胜荣幸。

一、创建compile_commands.json

compile_commands.json 文件能够有效提高一些工具(比如vscode)的代码跳转、补全等功能。

1、cmake中使用

cmake工程生成 compile_commands.json 文件比较简单:

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1

2、make中使用

安装bear:

sudo apt-get install bear

执行:

bear -- make -j8

会生成compile_commands.json文件

二、安装必要的插件

1.远程连接ssh

Remote-SSH

2.C/C++

在这里插入图片描述

3.C/C++ Extension Pack

在这里插入图片描述

4.Nsight Visual Studio Code Edition

在这里插入图片描述

5. vscode-cudacpp

在这里插入图片描述

三、json文件配置

1、配置c_cpp_properties.json

Ctrl+Shift+P搜索C/C++:Edit Configurations(JSON),点击进入:
在这里插入图片描述
随后生成.vscode文件:
在这里插入图片描述
c_cpp_properties.json配置为如下所示:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c17",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "linux-gcc-x64",
            "configurationProvider": "ms-vscode.makefile-tools",
            "compileCommands": "${workspaceFolder}/compile_commands.json"
        }
    ],
    "version": 4
}

“compileCommands”: "${workspaceFolder}/compile_commands.json"为新添加的内容。

配置路径也可以在includePath中进行配置:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/cuda/include/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c17",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "linux-gcc-x64",
            "configurationProvider": "ms-vscode.makefile-tools"
        }
    ],
    "version": 4
}

2、配置setting.json

在.vscode文件夹中创建setting.json文件,添加内容:

{
    "files.associations": {
        "*.cu":"cuda-cpp"
    }
}

参考查询网址vscode language identifier
相关博客:
博客一
博客二

3、配置tasks.json

1.Ctrl+Shift+P搜索Tasks:Configures Task,点击进入:
在这里插入图片描述
2.选择 使用模板创建tasks.json文件(可能是英文形式)
在这里插入图片描述
3.选择Others
在这里插入图片描述
最终tasks.json文件内容:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "make",
            "type": "shell",
            "command": "make -j16"
        }
    ]
}

cmake编译tasks.json:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "options": {
        "cwd": "${workspaceFolder}/build"
    },
    "tasks": [
        {
            "type":"shell",
            "label": "cmake",
            "command": "cmake",
            "args": [
                ".."
            ]
        },
        {
            "label": "make",
            "group":{
                "kind": "build",
                "isDefault": true
            },
            "command": "make",
            "args": [
                "-j8"
            ]
        },
        {
            "label": "Build",
            "dependsOrder": "sequence",     // 按照顺序执行
            "dependsOn":[
                "cmake",
                "make"
            ]
        }
    ]
}

4、配置launch.json

1.Ctrl+Shift+P搜索Debug:Add Configuration,点击进入:
在这里插入图片描述2.选择 CUDA C++(CUDA-GDB)
在这里插入图片描述
生成launch.json文件。

增加"program": “${workspaceFolder}/cudaAPP”,cudaAPP是编译出的可执行文件。

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "CUDA C++: Launch",
            "type": "cuda-gdb",
            "request": "launch",
            "program": "${workspaceFolder}/cudaAPP"
        },
        {
            "name": "CUDA C++: Attach",
            "type": "cuda-gdb",
            "request": "attach"
        }
    ]
}

相关博客
Cmake配置C++:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/test",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],

            "preLaunchTask": "Build",
            "miDebuggerPath": "/usr/bin/gdb"
        }

    ]
}

附录:vs code中的变量解释

以:home/s/test/.vscode/tasks.json为例
${workspaceFolder}:表示当前workspace文件夹路径,即home/s/test
${workspaceRootFolderName}:表示workspace的文件夹名字,即test
${file}:文件自身绝对路径,即home/s/test/.vscode/tasks.json
${relativeFile}:文件在workspace的路径,即.vscode/tasks.json
${fileBasenameNoExtension}:当前文件的文件名,不带后缀,即tasks
${fileBasename}:当前文件的文件名,即tasks.json
${fileDirname}:当前所在文件夹路径,即home/s/test/.vscode
${fileExtname}:当前文件的后缀,即.json
${lineNumber}:当前文件光标所在行数
${env:PATH}:系统中的环境变量

如果您觉得这篇文章对你有帮助,记得 点赞 + 关注 + 评论 三连,您只需动一动手指,将会鼓励我创作出更好的文章,快留下你的足迹吧💪💪💪

### 使用 VSCode 在 Ubuntu 上编译 CUDA 程序 #### 安装 Visual Studio Code 和必要的扩展 为了能够在 Ubuntu 上使用 VSCode 编译 CUDA 程序,首先需要安装 Visual Studio Code 及其相关插件。可以通过软件中心或者命令行来完成这一操作[^1]。 ```bash sudo snap install --classic code ``` 接着,在 VSCode 中打开 Extensions 视图 (Ctrl+Shift+X),搜索并安装 "C/C++" 扩展以及支持 CUDA 的开发工具包。 #### 配置 CUDA 开发环境 确保已经正确安装了 NVIDIA GPU 驱动程序和 CUDA Toolkit 后,还需要配置好 PATH 和 LD_LIBRARY_PATH 环境变量以便于访问 nvcc 编译器和其他库文件。可以在 `~/.bashrc` 文件中添加如下内容: ```bash export PATH=/usr/local/cuda/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH ``` 保存更改后通过 source 命令使设置生效: ```bash source ~/.bashrc ``` #### 创建项目结构与编写源码 创建一个新的工作区用于放置所有的源代码文件,并建立合理的目录层次结构。例如: ``` my_cuda_project/ ├── src/ │ └── main.cu └── CMakeLists.txt ``` 编辑 `main.cu` 来实现简单的 Hello World 应用程序作为测试案例。 ```cpp #include <stdio.h> __global__ void helloFromGPU() { printf("Hello from device!\n"); } int main(){ printf("Hello from host\n"); // Launch kernel on GPU. helloFromGPU<<<1, 1>>>(); // Wait for GPU to finish before accessing on host. cudaDeviceSynchronize(); return 0; } ``` 对于更复杂的工程,则建议采用 CMake 或者 Makefile 进行构建管理;这里仅提供了一个基本的例子说明如何启动一个新项目。 #### 设置 launch.json 和 tasks.json 为了让调试更加方便高效,应该定义合适的 task 和 launch configuration 。这通常涉及到两个 JSON 文件——tasks.json 和 launch.json ——它们位于 .vscode 文件夹之下。 在 `.vscode/tasks.json` 添加以下内容以指定编译任务: ```json { "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "/usr/local/cuda/bin/nvcc", "args": [ "-gencode=arch=compute_70,code=sm_70", "${workspaceFolder}/src/main.cu", "-o", "${workspaceFolder}/bin/hello" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"], "detail": "Generated task using Shell Command Provider." } ] } ``` 而针对 `.vscode/launch.json`, 则应包含类似下面这样的条目用来描述怎样去运行和调试应用程序: ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/bin/hello", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build", "miDebuggerPath": "/usr/bin/gdb", "logging": {"trace":true,"traceResponse":true}, "internalConsoleOptions": "openOnSessionStart" } ] } ``` 以上配置允许一键点击 F5 即可自动触发编译过程并将控制权交给 GDB 调试器来进行单步跟踪等操作[^3]。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值