---
# System prepended metadata

title: Vscode執行C++

---

# Vscode執行C++

1. **安裝[Vscode](https://code.visualstudio.com/)**
2. **安裝[MinGW](https://sourceforge.net/projects/mingw/files/)** (按`Download Latest Version`，檔名是mingw-get-setup.exe，MinGW其實就是C++編譯器)，然後install `mingw32-gcc-g++`和`mingw32-base`
3. **將MinGW的安裝路徑`C:\MinGW\bin`加入系統環境變數**
![](https://i.imgur.com/2WJf4ot.png)

4. **安裝Vscode的C/C++套件**
5. **建置json檔案**

先在專案資料夾中建立`.vscode`的資料夾，然後在其中建立以下幾個json檔

```json=
// c_cpp_properties.json，程式碼自動完成 (IntelliSense)
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "C:\\MinGW\\include",
                "C:\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include",
                "C:\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include\\c++"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=5",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "intelliSenseMode": "clang-x64",
            "browse": {
                "path": [
                    "${workspaceRoot}",
                    "C:\\MinGW\\include",
                    "C:\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include",
                    "C:\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include\\c++"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 3
}
// path 內的 C:\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include\\c++，要與安裝 MinGW 的路徑相符
```

```json=
// tasks.json，建置(Building)
{
    // 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
            }
        }
    ]
}

```

```json=
// launch.json，偵錯(Debugging)
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build"
        }
    ]
}
```