你已经下载并安装了 CUDA Toolkit 和 Visual Studio Build Tools(或 Visual Studio) ,接下来我们一步步在 VSCode 中配置 CUDA 开发环境 ,实现 编写 → 编译 → 运行 → 调试 的完整流程。
✅ 前提确认(请先验证)
在继续前,请确保以下命令在 "x64 Native Tools Command Prompt for VS 2022" 中能正常运行:
nvcc --version
cl
nvcc显示 CUDA 版本(如 12.4)cl显示 Microsoft C++ 编译器信息
如果不行,请先解决环境问题(如重启、重新安装 Build Tools 等)。
🛠️ 步骤 1:安装 VSCode 插件
打开 VSCode,安装以下插件:
- C/C++ (Microsoft 官方)
- 提供智能提示、调试支持
- CUDA (作者:kriegalex)
- 为
.cu文件提供语法高亮(可选但推荐)
- 为
其他插件如 CMake Tools、Code Runner 可后续按需添加。
📁 步骤 2:创建 CUDA 项目目录
例如:
D:\cuda_projects\hello_cuda\
├── main.cu
└── .vscode/
编写 main.cu 示例代码:
#include <cuda_runtime.h>
#include <iostream>
__global__ void say_hello() {
printf("Hello from GPU thread %d!\n", threadIdx.x);
}
int main() {
std::cout << "Launching kernel...\n";
say_hello<<<1, 5>>>();
cudaDeviceSynchronize();
// 检查错误(可选)
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
std::cerr << "CUDA error: " << cudaGetErrorString(err) << std::endl;
}
return 0;
}
⚙️ 步骤 3:配置 .vscode/tasks.json(编译)
在项目根目录创建 .vscode/tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "build CUDA",
"type": "shell",
"command": "nvcc",
"args": [
"-o",
"${workspaceFolder}/main.exe",
"${workspaceFolder}/main.cu",
"-arch=sm_75" // ⚠️ 根据你的 GPU 修改!
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$msCompile",
"detail": "Compile CUDA source with nvcc"
}
]
}
🔍 关于 -arch=sm_XX:
- 这是 GPU 的计算能力(Compute Capability)
- 常见值:
- GTX 10xx / RTX 20xx →
sm_75 - RTX 30xx →
sm_86 - RTX 40xx →
sm_89
- GTX 10xx / RTX 20xx →
- 查询你的 GPU:https://developer.nvidia.com/cuda-gpus
如果不确定,可先省略
-arch,但可能性能不佳或报错。
🐞 步骤 4:配置 .vscode/launch.json(调试/运行)
创建 .vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Run CUDA Program",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/main.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "externalTerminal"
}
]
}
使用
cppvsdbg是因为 Windows 上 VSCode 的 C++ 调试依赖 Visual Studio 的调试引擎(已随 Build Tools 安装)。
▶️ 步骤 5:编译并运行
-
编译:
- 按
Ctrl + Shift + B - 或终端手动运行:
nvcc -o main.exe main.cu -arch=sm_75
- 按
-
运行/调试:
- 按
F5启动调试(会弹出外部终端窗口) - 或直接双击
main.exe(在资源管理器中)
- 按
✅ 预期输出(以 sm_75 为例):
Launching kernel...
Hello from GPU thread 0!
Hello from GPU thread 1!
Hello from GPU thread 2!
Hello from GPU thread 3!
Hello from GPU thread 4!
🔧 可选优化:自动检测 nvcc 路径(避免环境变量问题)
如果你在普通 VSCode 终端中运行 nvcc 报错,但开发人员命令提示符中可以,说明环境变量未全局生效。
解决方案:
在 tasks.json 中使用完整路径(不推荐长期使用),或:
方法:从 VS 开发人员命令提示符启动 VSCode
-
先关闭vscode,打开 x64 Native Tools Command Prompt for VS 2022
-
进入你的项目目录:
cd D:\cuda_projects\hello_cuda -
启动 VSCode:
code .
这样 VSCode 继承了正确的环境变量,
nvcc和cl都能直接调用。
🧪 验证是否真正使用 GPU
在代码中加入错误检查(如上所示),或使用 nvidia-smi 监控:
- 打开任务管理器 → 性能 → GPU
- 运行程序时,应看到 GPU 计算负载上升
📌 常见问题
| 问题 | 解决 |
|---|---|
nvcc : fatal error : Microsoft Visual Studio not found |
未正确安装 Build Tools,或未用开发人员命令提示符启动 VSCode |
cannot open include file 'cuda_runtime.h' |
CUDA Toolkit 未装好,检查 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.x\include 是否存在 |
| 调试时提示 "Unable to start debugging" | 确保安装了 C++ 调试组件(Build Tools 中默认包含) |