VS Code 編譯C++


1.安裝VS Code

2.安裝插件

  在左側插件庫

  必須:

    c/c++ 插件

  非必需:

    C++ Intellisense

    Include Autocomplete

3.安裝編譯調試環境mingw

MinGW是是將GCC編譯器和GNU Binutils移植到Win32平台下的產物,包括一系列頭文件(Win32API)、庫和可執行文件。MinGW是從Cygwin(1.3.3版)基礎上發展而來。GCC支持的語言大多在MinGW也受支持,其中涵蓋C、C++、Objective-C、Fortran及Ada。

建議離線安裝MinGW  可參考

配置好環境變量

通過g++ xx.cpp -o xx 檢測是否可以用命令行來編譯c++文件,可以運行則安裝成功

4.調試環境配置

新建文件夾Test

新建.vscode文件夾

  在.vscode下創建配置文件c_cpp_properties.json、launch.json、tasks.json

  (1)c_cpp_properties.json

    配置文件指定mingw所在位置,方便vscode自動調用

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/tr1",
                "D:/mingw64/x86_64-w64-mingw32/include/"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "intelliSenseMode": "clang-x64",
            "browse": {
                "path": [
                    "${workspaceRoot}",
                    "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                    "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                    "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                    "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                    "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/tr1",
                    "D:/mingw64/x86_64-w64-mingw32/include/"
                ]
            },
            "compilerPath": "D:\\mingw64\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ],
    "version": 4
}

    (2)launch.json文件

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Launch (GDB)", // 配置名稱,將會在啟動配置的下拉菜單中顯示
            "type": "cppdbg", // 配置類型,這里只能為cppdbg
            "request": "launch", // 請求配置類型,可以為launch(啟動)或attach(附加)
            "targetArchitecture": "x86", // 生成目標架構,一般為x86或x64,可以為x86, arm, arm64, mips, x64, amd64, x86_64
            "program": "${file}.exe", // 將要進行調試的程序的路徑
            "miDebuggerPath": "d:\\mingw64\\bin\\gdb.exe", // miDebugger的路徑,注意這里要與MinGw的路徑對應
            "args": [
                "blackkitty",
                "1221",
                "# #"
            ], // 程序調試時傳遞給程序的命令行參數,一般設為空即可
            "stopAtEntry": false, // 設為true時程序將暫停在程序入口處,一般設置為false
            "cwd": "${workspaceRoot}", // 調試程序時的工作目錄,一般為${workspaceRoot}即代碼所在目錄
            "externalConsole": true, // 調試時是否顯示控制台窗口,一般設置為true顯示控制台
            "preLaunchTask": "g++" // 調試會話開始前執行的任務,一般為編譯程序,c++為g++, c為gcc
        }
    ]
}

    (3)tasks.json

{
    "version": "0.1.0",
    "command": "g++",
    "args": [
        "-g",
        "${file}",
        "-o",
        "${file}.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
        }
    }
}

  (4)在Test下新建測試文件test1.cpp

#include <iostream>
#include <windows.h>
using namespace std;

int main() {
    std::cout << "hello, I love you!" << std::endl;
    system("pause");
    return 0;
}

運行調試

左側的debug

輸出結果

 


免責聲明!

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



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