前置步驟
- 安裝符合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 PROPERTIESCUDA_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 PROPERTIESCUDA_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 easehttps://levelup.gitconnected.com/debugging-cuda-in-cmake-applications-on-vscode-with-ease-4a1990d77b18