VsCode配置C/C++開發環境

Blog發表於2022-02-08

Visual Studio Code(VS Code)是基於 Electron 開發,支援 Windows、Linux 和 macOS 作業系統。內建了對JavaScript,TypeScript和Node.js的支援並且具有豐富的其它語言和擴充套件的支援,功能超級強大。

第一步:安裝VsCode

首先需要到vscode官網下載vscode編輯器,並進行安裝。

安裝完成後,開啟vscode進入如下頁面,選擇C/C++官方外掛,點選install進行安裝

alt

第二步:編譯器安裝與配置

下面開始安裝配置C/C++編譯器

因為vscode只是一個程式碼編輯器,沒有自帶有C/C++的編譯器,因此需要安裝一個額外C/C++編譯器,這裡使用 MinGW-W64 GCC。因為網路問題mingw-w64很難下載,所以直接給出網盤下載地址。

下載地址:https://pan.baidu.com/s/1EhmVd97xFRtfy3V3sJzQlg

檔案提取碼:qghe

下載完成後將壓縮包移動到C:\Program Files中進行解壓,將mingw64中bin資料夾的路徑複製下來
alt

開啟系統環境變數,在Path中新建系統環境變數並將複製好的路徑新增進去,確定儲存後完成配置

alt

以上步驟完成後,驗證一下時候系統環境是否配置成功

快捷鍵Win+R開啟執行視窗,並輸入cmd,回車開啟命令提示符

在游標處輸入以下程式碼,後顯示如下圖所示,即完成系統環境配置。如果這裡出現問題,後面全部無法進行

gcc -v -E -x c++ -

alt

第三步:配置VsCode

1.先新建一個資料夾作為C專案檔案,然後點選VsCode選單欄中的File——>Open Folder,找到剛才新建的資料夾,然後點選選擇資料夾開啟這個專案檔案。

在資料夾中新建一個C檔案,並命名為Hello.c並輸入以下程式碼。

	#include<stdio.h>
	int main(){
    printf("helloworld");
    return 0;
}

alt

在建立一個.vscode資料夾(注意vscode前面要加一個.),並新建以下三個檔案

< c_cpp_properties.jsonlaunch.json、 tasks.json>

建立完成後將以下程式碼分別複製到對應檔案中

1. c_cpp_properties.json

    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "C:/Program Files/mingw64/include/**",
                "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": "",
                "path": [
                    "${workspaceRoot}",
                    "C:/Program Files/mingw64/include/**",
                    "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                    "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                    "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                    "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                    "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                    "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
                ]
            }
        }
    ],
    "version": 4
}

以下兩處需要根據真實環境中mingw64中bin資料夾的存放位置自行替換

No.1
alt

No.2
alt

2.launch.json

    "version": "0.2.0",
    "configurations": [
        {
            "name": "(Windows) Launch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "cmd",
            "preLaunchTask": "echo",
            "args": [
                "/C",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "&",
                "echo.",
                "&",
                "pause"
            ],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole":true
        },
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            // "externalConsole": true,
            "console":"externalTerminal",
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw64\\bin\\gdb.exe",
            "preLaunchTask": "echo",//這裡和task.json的label相對應
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
 
        }
    ]
}

注:將miDebuggerPath屬性裡的內容也要改成自己的路徑

3.tasks.json

    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "gcc",
            "args": [
                "-g", 
                "${file}", 
                "-o", 
                "${fileBasenameNoExtension}.exe",
                "-fexec-charset=GBK"//解決中文亂碼
            ]
        }
    ],
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared", 
        "showReuseMessage": true,
        "clear": false
    }
}

以上所有步驟全部完成後,開啟已經建立好的hello.c檔案,按F5鍵或點選右上角三角形的執行按鈕,執行程式,執行成功後會在console中顯示以下內容:

F5:
alt

執行按鈕:
alt

到此VsCode中配置C/C++開發環境全部結束

相關文章