Windows 下使用 VScode 運行 C/C++ 程序


下載安裝及配置

下載安裝 Visual Studio Code,安裝中將如下選項勾選,有助於后期使用方便,安裝位置自定義即可。

下載 mingw-w64,建議下載 離線版,下載完成后,找一個合適的位置(相當於安裝位置),解壓安裝包。例如本文演示地址為:C:/mingw64

進入解壓好的文件夾下的 bin 文件夾,將當前路徑(C:/mingw64/bin)復制下來,然后選中桌面上的此電腦,右擊鼠標,選擇屬性,選擇高級系統設置,點擊環境變量,選擇系統環境變量下的 Path,選擇編輯,點新建,然后把剛剛復制的文件路徑粘貼進去,然后點確定並退出。

按下鍵盤上的 Win+r,輸入 cmd,在彈出的對話框中,輸入:gcc -v

若出現 MinGW-W64 的提示(一堆英文),即為安裝成功:

如下有兩種編譯方法,第一種是用 VScode 的擴展 Code Runner 進行編譯,操作簡單,但是缺點是無法調試,只能用於簡單的編譯,適合快速測試一些簡單的程序。第二種是新建相關文件,搭建成一個環境進行編譯調試,相比於第一種方法,會稍顯麻煩。建議使用第二種方法更好。

第一種方法

在 VScode 里,安裝 C/C++ 和 Code Runner 這兩個擴展


安裝完成后,重啟 VScode,會在右上角找到一個三角形標志,這個標志就是用來編譯的。

接下來點擊 文件 > 首選項 > 設置 > 用戶設置 > 拓展 > Run Code Configuration,找到 Run In Terminal 選項並打上勾。這一步是為了解決程序中 scanf() 等請求鍵盤輸入數據的函數,在運行時無法從鍵盤輸入數據的問題。

完成后,按下鍵盤的 Ctrl+N 新建一個文件並保存,例如 helloworld.c 文件:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    printf("Hello World!\n");
    system("pause");
    return 0;
}

點擊 VScode 右上角的三角形,即可自動進行編譯運行。

第二種方法

編譯調試 C 過程

首先在你需要的位置新建一個文件夾,路徑不能有中文,例如本次演示路徑為:D:\test_c

打開 VScode ,安裝 C/C++ 擴展

安裝完成后,重啟 VScode,點擊 文件->打開文件夾,將剛才新建的文件夾添加到 VScode 里。然后在側邊欄里,右鍵選擇新建文件夾,命名為 .vscode,同樣的步驟再新建一個名稱為 build 的文件夾。

右鍵選中 .vscode 文件夾,選擇新建文件,命名為 c_cpp_properties.json,填入如下代碼:

注意 compilerPath 更改為你所安裝的路徑,注意斜杠的方向。

{
    "configurations": [
        {
            "name": "MinGW64",
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "C:/mingw64/bin/gcc.exe",
            "includePath": [
                "${workspaceFolder}"
            ],
            "cStandard": "c11"
        }
    ],
    "version": 4
}

同樣的步驟,右鍵選中 .vscode 文件夾,新建名稱為 launch.json 的文件,填入如下代碼:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C Launch (GDB)", // 配置名稱
            "type": "cppdbg",         // 配置類型
            "request": "launch",
            "targetArchitecture": "x64",
            "program": "${fileDirname}/build/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "externalConsole": false, // 調試時是否顯示控制台窗
            "internalConsoleOptions": "neverOpen",
            "MIMode": "gdb",
            "miDebuggerPath": "C:/mingw64/bin/gdb.exe", // 更改為你所安裝的路徑,注意斜杠的方向,別輸錯了!!!
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for GDB",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "compile" 
        }
    ]
}

右鍵選中 .vscode 文件夾,新建名稱為 tasks.json 的文件,填入如下代碼:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "compile",
            "command": "gcc",
            "args": [
                "${file}",
                "-o",
                "${fileDirname}/build/${fileBasenameNoExtension}.exe",
                "-O0",
                "-ggdb3",
                "-Wall",
                "-std=c11",
                "-Wno-format",
                "-finput-charset=UTF-8",
                "-fexec-charset=GB18030",
                "-D _USE_MATH_DEFINES"
            ],

            "type": "shell",
            
            "group": {
                "kind": "build",
                "isDefault": true
            },

            "presentation": {
                "echo": true,
                "reveal": "always",
                 "focus": false,
                 "panel": "shared"
            },
        }
    ]
}

完成后,效果如下:

右鍵點擊左側欄空白處,新建名為 hello_world.c 測試文件,填入如下測試代碼:

#include <stdio.h>
#include <stdlib.h>

int j = 0;

int main(void) {
    for (int i = 0; i < 4; i++) {
        j++;
        printf("Hello World!\n");
    }

    system("pause");
    return 0;
}

保存后,按下 F5 即可編譯。

如下為調試方法:

hello_world.c 文件里,找一個位置打斷點,例如在 for 循環這里:

然后按下 F5 進行編譯,它會自動停在斷點位置:

通過上方的按鈕進行操控即可。還可以選中某個變量,右鍵選擇添加到監視,就可以實時查看該變量的變化情況。

編譯調試 C++ 過程

和編譯調試 C 過程一樣,只需要修改 .vscode 文件里的代碼即可。

文件 c_cpp_properties.json 代碼更換為:

注意 compilerPath 更改為你所安裝的路徑,注意斜杠的方向。

{
    "configurations": [
        {
            "name": "MinGW64",
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "C:/mingw64/bin/g++.exe",
            "includePath": [
                "${workspaceFolder}"
            ],
            "cppStandard": "c++17"
        }
    ],
    "version": 4
}

文件 launch.json 代碼更換為:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Launch (GDB)",
            "type": "cppdbg",
            "request": "launch",
            "targetArchitecture": "x64",
            "program": "${fileDirname}/build/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "externalConsole": false,
            "internalConsoleOptions": "neverOpen",
            "MIMode": "gdb",
            "miDebuggerPath": "C:/mingw64/bin/gdb.exe",    // 更改為你所安裝的路徑,注意斜杠的方向,別輸錯了!!!
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for GDB",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "Compile" 
        }
    ]
}

文件 tasks.json 代碼更換為:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile",
            "command": "g++",
            "args": [
                "${file}",
                "-o",
                "${fileDirname}/build/${fileBasenameNoExtension}.exe",
                "-O0",
                "-ggdb3",
                "-Wall",
                "-static-libgcc",
                "-std=c++17",
                "-finput-charset=UTF-8",
                "-fexec-charset=GB18030",
                "-D _USE_MATH_DEFINES"
            ],

            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": [
                    "absolute",
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            },

            "type": "shell",
            
            "group": {
                "kind": "build",
                "isDefault": true
            },

            "presentation": {
                "echo": true,
                "reveal": "always",
                 "focus": false,
                 "panel": "shared" 
            },
        }
    ]
}

新建 CPP 測試文件 hello_world.cpp

#include <cstdlib>
#include <iostream>

using namespace std;

int j = 0;

int main(void) {

    for (int i = 0; i < 10; i++) {
        j++;
        cout << "Hello World!" << endl;
    }

    system("pause");
    return 0;
}

其他的操作都一樣了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM