vscode配置C++編譯和調試環境


1配置文件

一般vscode配置C++有三個文件,它們分別是:

1.1.c_cpp_properties.json

設置編譯環境參數。通過Ctrl+Shift+P,輸入C++,在下拉菜單中選擇“C/C++ Edit configuration”,系統自動會在.vscode目錄下創建該文件,供我們設置編譯環境。可根據自己需求改動如下配置,默認配置如下:

{
    "configurations": [
        {
            "name": "Win32", // 環境名稱
            "includePath": [ // 頭文件包含路徑,當前指定的是工作目錄,有需要可添加,加入相應的文件路徑即可
                "${workspaceFolder}/**"
            ],
            "defines": [     // 預處理定義
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\mingw64\\bin\\gcc.exe", // 編譯器路徑
            "cStandard": "gnu17",  // 設置C標准庫
            "cppStandard": "gnu++14", // 設置C++標准庫
            "intelliSenseMode": "gcc-x64" // 設置補全模式
        }
    ],
    "version": 4
}

 

1.2.tasks.json

設置編譯參數,通過Ctrl+Shift+p,輸入task,在下拉菜單中選擇Tasks: Configure Default Build Task -> Create task.json file from templates -> Others,系統自動在.vscode下創建task.json文件,供我們設置具體的編譯規則。根據實際請求修改如下:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build C++ train", // 當前編譯任務名字
            "type": "shell",
            "command": "g++",  // 編譯時執行的程序
            "args": ["-g", "${workspaceFolder}/*.cpp"  , "-o", "train"], // 傳遞給command的參數,也就是編譯參數
            "group": {
                "kind": "build",
                "isDefault": true // true,用戶可以通過Ctrl+Shift+B直接運行編譯任務
            }
        }
    ]
}

 

其中:${workspaceFolder}表示當前工作文件夾,加上/*.cpp表示多所有的.cpp文件進行編譯。

通過編譯之后,就會自動鏈接生成可執行文件。

3.launch.json

設置調試參數。通過Ctrl+Shift+P打卡命令行,輸入“lauch”選擇“Debug: Open launch.json” -> "C++(GDB/LLDB)",即可打開調試配置文件launch.json。配置后之后,按F5,進入調試模式,配置如下:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 啟動",
            "type": "cppdbg",
            "request": "launch",
            "program": "輸入程序名稱,例如 ${workspaceFolder}/a.exe", // 設置exe路徑
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/path/to/gdb", // 設置當前系統的gdb路徑,windows下是gdb.exe
            "setupCommands": [
                {
                    "description": "為 gdb 啟用整齊打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

  

進行Debug的前提,需要在可執行程序時加入了編譯選項-g。這樣gdb才可以調試。


免責聲明!

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



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