Windows中在VSCodeCursor上通过CMake或launch文件配置CUDA编程环境
Windows中在VSCode/Cursor上通过CMake或launch文件配置CUDA编程环境
前置步骤
- 安装符合GPU型号的CUDA Toolkit
- 配置好 nvcc 环境变量
- 安装 Visual Studio
- 参考
https://blog.csdn.net/Cony_14/article/details/137510909
- 参考
- VSCode 安装插件
Nsight Visual Studio Code Edition
- 注意:
不是
vscode-cudacpp
。若两个插件同时安装,会导致语法功能异常。
- 注意:
不是
- 安装 cmake 并配置好环境变量
注 :Windows 端笔者暂时没找到直接在VSCode中直接调试的方法,不过在Visual Studio中可以。
方法一:配置tasks和launch文件
- 文件-打开文件夹-选择
.cu
文件所在目录 - 点开侧边栏 运行与调试 按钮,点击创建launch.json文件,选择环境为CUDA C++(CUDA-GDB)
- 文件夹根目录下生成了一个
.vscode
目录,里面生成了一个launch.json
文件 - 手动在
.vscode
目录下创建tasks.json文件
tasks.json
文件内容如下:
{
"version": "2.0.0",
"tasks": [
{
"label": "mynvcc",
"type": "shell",
"command": "nvcc",
"args": [
"-o",
"${fileDirname}\\${fileBasenameNoExtension}",//VSCode里的宏,如果不了解可用直接copy,以工作区为默认路径
"${file}"//源文件
]//等同于nvcc -o /CodeDir/test test.cu
}
]
}
launch.json
文件内容如下:
{
"version": "0.2.0",
"configurations": [
{
"name": "CUDA C++: Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"console": "externalTerminal", //使用外部终端,如果是vscode的终端会似乎会根据type设置的调用调试导致闪退
"preLaunchTask": "mynvcc",
},
{
"name": "CUDA C++: Attach",
"type": "cuda-gdb",
"request": "attach"
}
]
}
- 我们只需要第一个
CUDA C++: Launch
- type
- 需要选择
cppvsdbg
。默认是cuda-gdb
在Windows上貌似不适配。
- 需要选择
- program
- 注意:需要
.exe
后缀
- 注意:需要
- preLaunchTask
- 在执行前先编译
- 填写
tasks.json
中label的名称
配置好后,即可直接在VSCode中运行CUDA代码。
方法二、配置CMake文件
- 文件-打开文件夹-选择
.cu
文件所在目录 - 根目录新建
CMakeLists.txt
文件
CMakeLists.txt
文件内容如下:
cmake_minimum_required(VERSION 3.20)
project(cuda_test CUDA)
set(CMAKE_CUDA_STANDARD 17)
link_directories(${LIB_DIR})
add_executable(cuda_test test.cu)
set_target_properties(cuda_test PROPERTIES
CUDA_SEPARABLE_COMPILATION ON)
project
和add_executable
中的cuda_test
- 自定义的项目名称
add_executable
中的test.cu
- 即:需要编译的CUDA代码( 需修改 成自己的)
查询编译器
- 在
terminal
中运行cmake -B build -G
- 会列出一系列生成器,复制自己安装的版本,如
"Visual Studio 16 2019"
- 会列出一系列生成器,复制自己安装的版本,如
编译运行
- 依次运行
cmake -B build -G "Visual Studio 16 2019"
cmake --build build
cd build\Debug
.\cuda_test.exe
步骤自动化
- 在项目根目录下创建文件
build_and_run.bat
setlocal
REM 清理 build 目录
if exist build (
rmdir /s /q build
echo Cleaned up build directory.
)
REM 创建 build 目录
mkdir build
echo Created build directory.
REM 使用 CMake 进行配置
cmake -B build -G "Visual Studio 16 2019"
if ERRORLEVEL 1 (
echo CMake configuration failed.
exit /b %ERRORLEVEL%
)
REM 构建项目
cmake --build build
if ERRORLEVEL 1 (
echo Build failed.
exit /b %ERRORLEVEL%
)
REM 进入 Debug 目录并运行测试
cd build\Debug
if ERRORLEVEL 1 (
echo Failed to enter Debug directory.
exit /b %ERRORLEVEL%
)
REM 运行
.\cuda_test.exe
endlocal
- 终端-运行任务-CMake生成
- 自动在根目录创建
.vscode
目录及tasks.json
文件
- 自动在根目录创建
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build, Run and Clean CUDA Test",
"type": "shell",
"options": {
"cwd": "${workspaceFolder}" // 确保命令在当前工作目录中执行
},
"command": "cmd",
"args": [
"/c",
"build_and_run.bat" // 调用合并的批处理脚本
],
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
- 编译并运行
- 终端-运行任务-
Build, Run and Clean CUDA Test
Build, Run and Clean CUDA Test
是tasks.json
文件中的lable
- 终端-运行任务-
Cursor
- 配置方法与VSCode一致。
- 在VSCode中已经配置好的项目,在Cursor中直接打开,可以用一样的方法在"Terminal - Run Task" 中运行。
编译过程中warning信息处理
- 警告1:
LINK : warning LNK4098: 默认库"LIBCMT"与其他库的使用冲突;请使用 /NODEFAULTLIB:library
- 原因:
- CUDA代码可能使用了静态多线程运行库(LIBCMT),而项目其他部分使用了动态调试多线程运行库(MDd)
- 解决方案:
CMakeLists.txt最后加上如下内容:
if(MSVC) set_target_properties(cuda_test PROPERTIES LINK_FLAGS "/NODEFAULTLIB:LIBCMT /NODEFAULTLIB:MSVCRT") endif()
或CMakeLists.txt修改成如下内容:
cmake_minimum_required(VERSION 3.20)
project(cuda_test CUDA)
set(CMAKE_CUDA_STANDARD 17)
# 确保CUDA编译器使用正确的运行时设置
string(APPEND CMAKE_CUDA_FLAGS " --use-local-env")
string(APPEND CMAKE_CUDA_FLAGS " -Xcompiler=\"/MDd\"")
# 如果是Release模式则使用MD
if(CMAKE_BUILD_TYPE STREQUAL "Release")
string(REPLACE "/MDd" "/MD" CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS}")
endif()
link_directories(${LIB_DIR})
add_executable(cuda_test test.cu)
set_target_properties(cuda_test PROPERTIES
CUDA_SEPARABLE_COMPILATION ON)
# 显式添加链接选项
if(MSVC)
set_target_properties(cuda_test PROPERTIES LINK_FLAGS "/NODEFAULTLIB:LIBCMT /NODEFAULTLIB:MSVCRT")
endif()
参考文献:
[1] windows下用vscode编译并运行cuda程序
https://zhuanlan.zhihu.com/p/567996994
[2] CUDA 番外篇 | Visual Studio Code的CUDA环境
https://zhuanlan.zhihu.com/p/508810115
[3] windows下使用vccode+cmake编译cuda程序
https://blog.csdn.net/threestooegs/article/details/135173376
[4] CUDA Programming in VS Code with CMake
https://levelup.gitconnected.com/debugging-cuda-in-cmake-applications-on-vscode-with-ease-4a1990d77b18
[5] 如何应用 VS Code,CMake 和 Make 编译 C ++ 代码?
https://zhuanlan.zhihu.com/p/354070726
[6] Debugging CUDA in CMake applications on VSCODE with ease
https://levelup.gitconnected.com/debugging-cuda-in-cmake-applications-on-vscode-with-ease-4a1990d77b18