---
# System prepended metadata

title: Windows VSCode C/C++環境建立
tags: [C]

---

# Windows VSCode C/C++環境建立
###### tags： `C`
## 系統環境
>OS：Windows11
>位元：x64

## 1. 下載VSCode
[官方連結](https://code.visualstudio.com/Download)
## 2. 安裝插件
搜尋C/C++ Extension Pack

![image](https://hackmd.io/_uploads/SyhypeagA.png)


搜尋 Code Runner

![image](https://hackmd.io/_uploads/B1b52lTgC.png)

## 3. 安裝7-zip
> 後續解壓縮用

[官方連結](https://7-zip.org/)
## 4. 安裝編譯器
### 版本差異說明
[版本差異說明](https://blog.csdn.net/B11050729/article/details/132176767)
### 下載
[下載連結](https://github.com/niXman/mingw-builds-binaries/releases)
我是下載這個版本：x86_64-13.2.0-release-posix-seh-ucrt-rt_v11-rev1.7z
### 解壓縮
找到`bin`的位置，複製路徑
![image](https://hackmd.io/_uploads/ry-FmxplR.png)

### 加入環境變數
#### 搜尋環境變數
![image](https://hackmd.io/_uploads/SkRg4e6g0.png)
#### 點選環境變數
![image](https://hackmd.io/_uploads/HyjPNx6lC.png)
#### 編輯
找到`系統環境變數` -> 點選`Path` -> 編輯
![image](https://hackmd.io/_uploads/Sk8pNlTx0.png)
#### 加入環境變數
點選`新增` -> 填入`bin`的路徑 -> 確認
![image](https://hackmd.io/_uploads/SJS-UxagR.png)
全部都確定，確保變更環境變數
## 檢查是否安裝成功
在終端機輸入
```
gcc -v
```
輸出版本等資訊
![image](https://hackmd.io/_uploads/rJo4wg6l0.png)

## 簡單程式測試
### 建立程式
開啟vscode -> 建立`main.c`
``` C
#include <stdio.h>

int main()
{
    printf("Hello world!!");
    return 0;
}
```

### 編譯成`exe`
點選上方`Terminal` -> `run Task`

![image](https://hackmd.io/_uploads/B1jMqgpeR.png)

選取第1個進行編譯

![image](https://hackmd.io/_uploads/H1AYqlTlR.png)

產生`main.exe`
#### 驗證`main.exe`

- `Terminal`輸入
```
./main.exe
```
- 輸出
```
Hello world!!
```

### 直接執行
點選由上方箭頭 -> Run code
![image](https://hackmd.io/_uploads/H1hFpeagC.png)

`Terminal` -> OUTPUT輸出結果

![image](https://hackmd.io/_uploads/HyylCg6e0.png)

## 中文亂碼解決
[參考連結_B3回覆](https://www.dcard.tw/f/softwareengineer/p/240541323)

修改`tasks.json`的`"args"`

![image](https://hackmd.io/_uploads/SJKSNCFG0.png)

原先
```json
{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: gcc.exe build active file",
			"command": "D:\\C_Learn\\x86_64-13.2.0-release-posix-seh-ucrt-rt_v11-rev1\\mingw64\\bin\\gcc.exe",
			"args": [
				"-fdiagnostics-color=always",
				"-g",
				"${file}",
				"-o",
				"${fileDirname}\\${fileBasenameNoExtension}.exe"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build",
			"detail": "compiler: D:\\C_Learn\\x86_64-13.2.0-release-posix-seh-ucrt-rt_v11-rev1\\mingw64\\bin\\gcc.exe"
		}
	]
}
```

在`args`新增`"-fexec-charset=BIG5"`

```json
{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: gcc.exe build active file",
			"command": "D:\\C_Learn\\x86_64-13.2.0-release-posix-seh-ucrt-rt_v11-rev1\\mingw64\\bin\\gcc.exe",
			"args": [
				"-fexec-charset=BIG5",
				"-fdiagnostics-color=always",
				"-g",
				"${file}",
				"-o",
				"${fileDirname}\\${fileBasenameNoExtension}.exe"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build",
			"detail": "compiler: D:\\C_Learn\\x86_64-13.2.0-release-posix-seh-ucrt-rt_v11-rev1\\mingw64\\bin\\gcc.exe"
		}
	]
}
```
## 參考連結
* [[C語言][教學] 開發準備#01-1. [Windows] 安裝與使用 VSCode 和 MinGW 撰寫開發 C 語言](https://www.youtube.com/watch?v=Lm_XYFRwxPw&t=264&ab_channel=ProgressBar%E9%80%B2%E5%BA%A6%E6%A2%9D%E7%B7%9A%E4%B8%8A%E8%AA%B2%E7%A8%8B)
* [參考連結_B3回覆](https://www.dcard.tw/f/softwareengineer/p/240541323)
