# 1. CUDA + VSCode (Recommended) I recommend the combination of cuda and VSCode for cuda programming, for the convenience I gained from it. Here are some useful blogs. - [Visual Studio Code的CUDA环境](https://zhuanlan.zhihu.com/p/508810115) (Recommended) - [使用VSCode搭建WSL + CUDA + C/C++开发环境](https://tieba.baidu.com/p/8014932367) - [CUDA开发环境搭建](https://zhuanlan.zhihu.com/p/488518526) ## 1.1. Install extensions CUDA is designed based on C/C++, so you need to install extension `C/C++`. But for now, VSCode can only recognize file formats like `.c` and `.cpp`. Thus, extension `Nsight Visual Studio Code Edition` should be installed to support recognition of file format `.cu`. - C/C++ - Nsight Visual Studio Code Edition ## 1.2. Configurations of the workspace To compile cuda files and run test tasks, file `taks.json` is needed. Also, file `launch.json` is a must to debug cuda files. I paste the template for those two files below for your convenience. You should create a folder `.vscode` in the work directory, and put two files in it. The file can be organized like this. ``` ./ ├── .vscode │   ├── tasks.json │   ├── launch.json ├── cudaByExample │   ├── chapter03 │   │   ├── enum_gpu │   │   ├── enum_gpu.cu │   │   ├── hello_world │   │   ├── hello_world.cu ├── mycuda │   ├── task1 │   │   ├── corr.cu ... ``` tasks.json: ```json { "tasks": [ /////////////////////////////////// cudaBuild ////////////////////////////////// { "label": "cudaBuild", "detail": "Build for cuda c", "group": { "kind": "build", "isDefault": true, }, "type": "cppbuild", "command": "nvcc", "args": [ "${file}", "-g", // "-lglut", "-lGLU", "-lGL", // include lib for graphics "-o","${fileDirname}/${fileBasenameNoExtension}"] }, /////////////////////////////////// run //////////////////////////////////////// { "label": "run", // Name of the task "detail": "Compile the file, then run it. Typical run task for cpp", "dependsOn": "cudaBuild", // Run compilation task `cudaBuild` before this test task "group": { "kind": "test", // compared to "build" "isDefault": true // linked to the button `Run the C\C++` / command `Run tasks: Run test tasks` // whose shortkey can be set to "F4" }, "type": "shell", "command": "${fileDirname}/${fileBasenameNoExtension}", "args": [], // Warning "Matches multiple schemas when only one must validate." // will show if this object ommited "presentation": { "focus": true, // Default is false. Nonsenseless for build. // Controls whether the panel takes focus. "panel": "new", // Default is shared. // Controls if the panel is shared between tasks, // dedicated to this task or a new one is created on every run. }, }, ], "version": "2.0.0" } ``` launch.json: ```json { "configurations": [ { /////////////////////////////////// cudaDebug ////////////////////////////////// { "name": "cudaDebug", "type": "cuda-gdb", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}", "preLaunchTask": "cudaBuild", // The task run before debugging }, ], "version": "2.0.0" } ``` ## 1.3. Validation of the configuration Download the repository from [CUDA by Example](https://github.com/CodedK/CUDA-by-Example-source-code-for-the-book-s-examples-) , open one of the cuda scripts in chapter 3 then click the button `Run C/C++ file` on the upper right corner. Hopefully, you will compile and run it successfully. # 2. Is VSCode a must? If you check the contents of file `tasks.json` carefully, you may find that all you need to compile a cuda script is the command `nvcc example.cu`. Then an executable file named `a.out` will be generated and you can run it by the command `./a.out`. If you want to modify the name of the executable file, you will need the command `nvcc example.cu -o exeFileName`. All in all, program `nvcc` will satisfy all your requirements.