1. 程式人生 > 其它 >VSCode配置C/C++環境(Win10系統)

VSCode配置C/C++環境(Win10系統)

技術標籤:安裝與使用c++visual studio codewindowsc語言

經過多次常識,找到了一種簡單高效的方式。
參考:2200年了還在用Dev嗎? 快來使用vscode配置高效-美觀-簡潔的c/c++程式設計環境

步驟:

  • 下載VSCode軟體,較為簡單不再贅述。中文外掛等非必須外掛可自行百度
  • 安裝外掛:Bracket Pair Colorizer 2、C/C++、Code Runner(直接搜尋安裝即可)
  • 由於上述軟體為編輯器,而非編譯器,因此需要安裝編譯環境。下載地址:MinGW-w64 - for 32 and 64 bit Windows並搜尋x86_64-posix-seh
    安裝即可。最後在PATH中配置環境變數(你的安裝目錄\mingw64\bin
  • 建立一個工作目錄,用來存放後續C/C++配置檔案以及原始檔。在該工作目錄下建立.vscode檔案,並建立如下圖所示檔案。
    其中每個檔案內容如下:
    launch.json:
// https://github.com/Microsoft/vscode-cpptools/blob/master/launch.md
{
    "version": "0.2.0",
    "configurations": [{
        "name": "(gdb) Launch", // 配置名稱,將會在啟動配置的下拉選單中顯示
        "type": "cppdbg", // 配置型別,cppdbg對應cpptools提供的除錯功能;可以認為此處只能是cppdbg
        "request": "launch", // 請求配置型別,可以為launch(啟動)或attach(附加)
        "program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 將要進行除錯的程式的路徑
        "args": [], // 程式除錯時傳遞給程式的命令列引數,一般設為空即可
        "stopAtEntry": false, // 設為true時程式將暫停在程式入口處,相當於在main上打斷點
        "cwd": "${workspaceFolder}", // 除錯程式時的工作目錄,此為工作區資料夾;改成${fileDirname}可變為檔案所在目錄
        "environment": [], // 環境變數
        "externalConsole": false, // 為true時使用單獨的cmd視窗,與其它IDE一致;18年10月後設為false可呼叫VSC內建終端
        "internalConsoleOptions": "neverOpen", // 如果不設為neverOpen,除錯時會跳到“除錯控制檯”選項卡,你應該不需要對gdb手動輸命令吧?
        "MIMode": "gdb", // 指定連線的偵錯程式,可以為gdb或lldb。但我沒試過lldb
        "miDebuggerPath": "gdb.exe", // 偵錯程式路徑,Windows下字尾不能省略,Linux下則不要
        "setupCommands": [
            { // 模板自帶,好像可以更好地顯示STL容器的內容,具體作用自行Google
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": false
            }
        ],
        "preLaunchTask": "Compile" // 除錯會話開始前執行的任務,一般為編譯程式。與tasks.json的label相對應
    }]
}

settings.json:

{
    "files.defaultLanguage": "c", // ctrl+N新建檔案後預設的語言
    "editor.formatOnType": true,  // 輸入分號(C/C++的語句結束標識)後自動格式化當前這一行的程式碼
    "editor.suggest.snippetsPreventQuickSuggestions": false, // clangd的snippets有很多的跳轉點,不用這個就必須手動觸發Intellisense了
    "editor.acceptSuggestionOnEnter": "off", // 我個人的習慣,按回車時一定是真正的換行,只有tab才會接受Intellisense
    // "editor.snippetSuggestions": "top", // (可選)snippets顯示在補全列表頂端,預設是inline

    "code-runner.runInTerminal": true, // 設定成false會在“輸出”中輸出,無法輸入
    "code-runner.executorMap": {
        "c": "cd $dir && gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'",
        "cpp": "cd $dir && g++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'"
        // "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt.exe -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && $dir$fileNameWithoutExt",
        // "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt.exe -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && $dir$fileNameWithoutExt"
    }, // 右鍵run code時執行的命令;未註釋的僅適用於PowerShell(Win10預設),檔名中有空格也可以編譯執行;註釋掉的適用於cmd(win7預設),PS和bash也能用,但檔名中有空格時無法執行
    "code-runner.saveFileBeforeRun": true, // run code前儲存
    "code-runner.preserveFocus": true,     // 若為false,run code後游標會聚焦到終端上。如果需要頻繁輸入資料可設為false
    "code-runner.clearPreviousOutput": false, // 每次run code前清空屬於code runner的終端訊息,預設false
    "code-runner.ignoreSelection": true,   // 預設為false,效果是滑鼠選中一塊程式碼後可以單獨執行,但C是編譯型語言,不適合這樣用

    "C_Cpp.clang_format_sortIncludes": true,
    "files.associations": {
        "array": "cpp",
        "atomic": "cpp",
        "*.tcc": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "unordered_map": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "algorithm": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "optional": "cpp",
        "string": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "fstream": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "iostream": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "new": "cpp",
        "ostream": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "typeinfo": "cpp"
    }, // 格式化時調整include的順序(按字母排序)
}

tasks.json:

// https://code.visualstudio.com/docs/editor/tasks
{
    "version": "2.0.0",
    "tasks": [{
        "label": "Compile", // 任務名稱,與launch.json的preLaunchTask相對應
        "command": "g++",   // 要使用的編譯器,C++用g++
        "args": [
            "${file}",
            "-o",    // 指定輸出檔名,不加該引數則預設輸出a.exe,Linux下預設a.out
            "${fileDirname}/${fileBasenameNoExtension}.exe",
            "-g",    // 生成和除錯有關的資訊
            "-Wall", // 開啟額外警告
            "-static-libgcc",     // 靜態連結libgcc,一般都會加上
            "-fexec-charset=GBK", // 生成的程式使用GBK編碼,不加這一條會導致Win下輸出中文亂碼
            // "-std=c11", // C++最新標準為c++17,或根據自己的需要進行修改
        ], // 編譯的命令,其實相當於VSC幫你在終端中輸了這些東西
        "type": "process", // process是vsc把預定義變數和轉義解析後直接全部傳給command;shell相當於先開啟shell再輸入命令,所以args還會經過shell再解析一遍
        "group": {
            "kind": "build",
            "isDefault": true // 不為true時ctrl shift B就要手動選擇了
        },
        "presentation": {
            "echo": true,
            "reveal": "always", // 執行任務時是否跳轉到終端面板,可以為always,silent,never。具體參見VSC的文件
            "focus": false,     // 設為true後可以使執行task時焦點聚集在終端,但對編譯C/C++來說,設為true沒有意義
            "panel": "shared"   // 不同的檔案的編譯資訊共享一個終端面板
        },
        // "problemMatcher":"$gcc" // 此選項可以捕捉編譯時終端裡的報錯資訊;但因為有Lint,再開這個可能有雙重報錯
    }]
}

無需進行更改(當然也可以根據自己需求更改)。

至此完成所有配置~

附測試C++程式碼(同時上述配置適合C程式碼):

#include <iostream>

using namespace std;
int main()
{
    cout << "Hello, world!" << endl;
    return 0;
}