天天看點

VSCode C++環境配置

VSCode C++環境配置

安裝軟體以及插件不贅述。

下面記錄一下C++環境配置檔案的配置。

在代碼的目錄下的.vscode檔案夾下建立launch.json、tasks.json

launch.json

{
    // 使用 IntelliSense 了解相關屬性。 
    // 懸停以檢視現有屬性的描述。
    // 欲了解更多資訊,請通路: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - 生成和調試活動檔案", // 配置名稱,将會在啟動配置的下拉菜單中顯示
            "type": "cppdbg",                       // 配置類型,這裡隻能為cppdbg
            "request": "launch",                        // 請求配置類型,可以為launch(啟動)或attach(附加)
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",// 将要進行調試的程式的路徑
            "args": [],                                         // 程式調試時傳遞給程式的指令行參數,一般設為空即可
            "stopAtEntry": false,                               // 設為true時程式将暫停在程式入口處,一般設定為false
            "cwd": "${workspaceRoot}",// 調試程式時的工作目錄,一般為${workspaceRoot}即代碼所在目錄
            "environment": [],
            "externalConsole": true,// 調試時是否顯示控制台視窗,一般設定為true顯示控制台
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw64\\bin\\gdb.exe",// miDebugger的路徑,注意這裡要與MinGw的路徑對應
            "setupCommands": [
                {
                    "description": "為 gdb 啟用整齊列印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++"// 調試會話開始前執行的任務,一般為編譯程式,c++為g++, c為gcc
        }
    ]
}
           

需要注意的是miDebuggerPath的值是你自己環境下minGW的安裝目錄。Tip:注意需要兩個反斜杠

{
    "version": "2.0.0",
    "tasks": [{
            "label": "g++",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
           
#include <iostream>
using namespace std;
int main()
{
    cout << "Hello Vscode" << endl;
    return 0;
}