---
# System prepended metadata

title: VS code 與 gdb 配置

---

# VS code 配置

# 1.下載並安裝 VS code
https://code.visualstudio.com/
# 2.安裝git
https://git-scm.com/
設為預設終端機
![image](https://hackmd.io/_uploads/BJbmd1Qwye.png)
![image](https://hackmd.io/_uploads/Sy38_kmw1g.png)
# 3.安裝MinGW-w64
https://www.mingw-w64.org/
![image](https://hackmd.io/_uploads/HybY6yQw1e.png)
![image](https://hackmd.io/_uploads/HJu9TkXPkl.png)
![image](https://hackmd.io/_uploads/Hyk6Ty7wkx.png)
![image](https://hackmd.io/_uploads/HyF1CkXvye.png)
![image](https://hackmd.io/_uploads/HkV-017vye.png)
下載完後雙擊解壓
![image](https://hackmd.io/_uploads/rkWER1XDke.png)
會得到一個w64devkit資料夾
![image](https://hackmd.io/_uploads/HJeDCy7vJe.png)
把他放到C:\下
加進環境變數
![image](https://hackmd.io/_uploads/rkyAAyXPJx.png)
![image](https://hackmd.io/_uploads/HkvlkgmD1l.png)
![image](https://hackmd.io/_uploads/rJCf1e7P1g.png)
點一下Path讓他反白，然後按編輯
![image](https://hackmd.io/_uploads/BJO-xlQD1x.png)
![image](https://hackmd.io/_uploads/SkJXeeXD1e.png)
填入路徑
![image](https://hackmd.io/_uploads/BySule7vkl.png)
![image](https://hackmd.io/_uploads/rJ29llmw1x.png)
到了上一個面板也要按一次確定，修改才會生效
![image](https://hackmd.io/_uploads/rkbRgg7w1x.png)
之後就可以自己用gcc或g++指令編譯了
![image](https://hackmd.io/_uploads/HJP2-emPkg.png)
範例程式 hello.c
```c
#include <stdio.h>

int g = 0;

int test_function_2(){
    printf("In the test function 2\n");
    g++;
}

int test_function_1(int *n){
    printf("In the test function 1\n");
    *n += 1;
    int b = 0;
    test_function_2();
}



int main(){
    int a = 0;
    a++;
    printf("hello world\n");
    test_function_1(&a);
    return 0;
}
```

使用以下指令編譯
```bash
gcc -g -Wall hello.c
```

# 4.配置gdb
進入調試面板
![image](https://hackmd.io/_uploads/HygE7eQvkg.png)
新建一個設定文件
![image](https://hackmd.io/_uploads/HyPUQl7wJe.png)
選擇 gdb
![image](https://hackmd.io/_uploads/SJFYmxQwJx.png)

填入以下配置
```json
"configurations": [
        {
            "name": "GDB Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.exe", // 可執行文件路徑
            "cwd": "${workspaceFolder}",
            "MIMode": "gdb",
            "miDebuggerPath": "C:/w64devkit/bin/gdb.exe", // GDB 的完整路徑
            "externalConsole": true,
            "stopAtEntry": true,
            "args": [], // 程式執行時的參數
            "setupCommands": [
                {
                    "description": "啟用漂亮打印（pretty-printing）",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
        }
    ]
```

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

之後側欄的調試介面應該會變這樣
![image](https://hackmd.io/_uploads/S18jiZ7vye.png)
然後按下這個符號就會開始
![image](https://hackmd.io/_uploads/H1cRsbQDkg.png)
會出現這個面板可以控制gdb前進的方式
![image](https://hackmd.io/_uploads/Bywm3-Xvkg.png)
旁邊的面板可以觀察各種變化的訊息
![image](https://hackmd.io/_uploads/SJWFhZ7DJl.png)












