---
title: VSCode推坑文
description: VSCode的C/C++配置
image: https://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Visual_Studio_Code_1.35_icon.svg/1200px-Visual_Studio_Code_1.35_icon.svg.png
tags: VSCode, C++, C, Visual Studio Code
---
# VSCode推坑文
## 0. 什麼是VSCode,為什麼選擇VSCode?
全名 = Visual Studio Code (文字編緝器,可調校成最好用的IDE)
- 跨平台
- 輕量化
- 擴充性高
- 超級好看 (重點!)

比Visual Studio (笨重IDE) 的開啟速度快,編譯也快
比Code Block, DevC++ (輕型IDE) 好看100倍,插件也多
這麼香還不試試看嗎?下面就來把VSCode裝起來!
## 1. 安裝VSCode
先到[官網](https://code.visualstudio.com/)下載安裝自己系統的版本:

安裝完後打開:

(下面說明將以Windows為主,其他系統可以自己上網查詢,關鍵字:VSCode C++ 系統名稱)
## 2. 好用Extension推薦
左邊Panel最底下就是插件區:

在這裡推薦幾個好用的插件:
1. C/C++:Microsoft官方的C/C++多功能插件 (裝完就很像IDE了)

2. Atom One Dark Theme:官方主題之外的好選擇

3. Chinese (Traditional) Language Pack for Visual Studio Code:
很討厭英文的可以試試

## 3. 安裝MinGW
[什麼是MinGW?Wiki連結](https://zh.wikipedia.org/wiki/MinGW) (簡短來說就是Windows版的C/C++編譯器)
[下載連結](https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/installer/mingw-w64-install.exe)
下載完後打開,Setting這樣選 (64位元的):

安裝目錄設定成`C:\mingw-w64`:

接下來就等安裝完成
## 4. 設定環境變數
左下角Windows圖標右鍵,選「系統」-> 「進階系統設定」
-> 「環境變數」-> 「系統變數」裡的Path -> 「編輯」
-> 「新增」兩個Path:
( xxx 以你的路徑為主,可以進檔案總管看看)
1. `C:\mingw-w64`
2. `C:\mingw-w64\xxx\bin`
**重新啟動**電腦,讓環境變數生效
(可以在cmd輪入`gcc -v`看看有沒有生效)






## 5. C/C++ Compile Run
安裝C/C++ Compile Run插件

安裝完後,點「齒輪」-> 「Extension Settings」

這個給他勾起來:

搞定!
## 6. Hello World!
到這裡就大功告成啦!右鍵New File來試試看吧

`helloworld.c`:
```c=
# include <stdio.h>
int main(){
printf("Hello World!");
}
```
按`Ctrl + 6`執行:

大功告成!
## 附錄:如何Debug
首先我們先配置C/C++ 設定檔
- Open Folder,挑一個你喜歡的資料夾

- 按下Ctrl+Shift+P,輸入edit找到Edit configurations(UI)

- 進入頁面後取名Configuration name為Win32
- 選擇compiler path為mingw-w64內部資料夾bin中的g++.exe

- IntelliSense模式請設定為gcc-x64

- 最後就會生成c_cpp_properties.json
- 在.vscode中建立tasks.json,內容如下:

```json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g", "${file}", "-o", "${fileBasenameNoExtension}.exe"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
```
- 先點蟲蟲圖標,在這邊點`create a launch.json file`:

- 選擇C++(GDB/LLDB)

- launch.json更改成下面這樣 (miDebuggerPath要再核對一下是不是正確的):
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${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"
}
]
}
```
接下來在`helloworld.c`設個Break point按`F5`:

在蟲蟲那個圖標就有Debug視窗了!