# Setup C/C++ environment for VScode (on Windows) ###### tags: `csst`, `setup`, `environment` ## 安裝 mingw 1. 下載 [mingw-w64](https://sourceforge.net/projects/mingw-w64/files/) ![](https://i.imgur.com/2atKFBv.png) 2. 如果是64位元電腦 - Architecture: x86_64 - Exception: seh ![](https://i.imgur.com/Wn2azHw.png) 3. 修改安裝路徑,==**切記路徑中不能有空格**== ![](https://i.imgur.com/UZ3pr38.png) 4. 等待安裝完畢。 5. 新增編譯器環境變數 1. 右鍵"本機" -> "內容" 2. 開啟"進階系統設定" 3. 開啟"環境變數" 4. 雙擊"系統變數 - Path" 5. 新增環境變數,"{mingw安裝路徑}\mingw64\bin" 6. 按"確定" ![](https://i.imgur.com/Y2tb5pv.png) ## 安裝 VScode 1. 下載 [VScode](https://code.visualstudio.com/Download) 2. 到 Extension 設定頁面 ![](https://i.imgur.com/w7bZMRo.png) 3. Install `C/C++` and `C++ Intellisense` Extensions. 4. ![](https://i.imgur.com/5wgD0VQ.png) ## (可選)Install Code Runner > 方便執行code,但沒有debugger功能 ### Setup 1. Install `Code Runner` Extension. 2. `File` -> `Preferences` -> `Settings` 3. Search for `Code Runner` 4. Set following options - [x] `Ignore Selection` - [x] `Run in terminal` - [x] `Save file before run` - [ ] `Preserve Focus` ### How to use - Run shortcut: `ctrl + alt + n` - Run button: running icon on up-right ## 設定 Debugger - add `.vscode/tasks.json` ```json { // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "g++", "args": [ "-g", "${file}", "-o", "${fileBasenameNoExtension}.exe" ], "group": { "kind": "build", "isDefault": true } } ] } ``` - add `.vscode/launch.json` ```json { // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceRoot}/${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "C:\\Mingw-w64\\mingw64\\bin\\gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build" } ] } ```