mac vscode c++自動編譯調試執行

##VScode拓展包
在這裏插入圖片描述
##新建cpp文件


##配置編譯文件tasks.json
快捷鍵“⇧⌘B”



###配置內容如下
其中注意label,與之後的自動調試執行launch.jsonpreLaunchTask有關

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "g++",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileBasenameNoExtension}.out"
            ],
            "problemMatcher": {  //正則匹配,刪除無用符號
                "owner": "cpp",
                "fileLocation": ["relative", "${workspaceRoot}"],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
    ]
}

##Debug配置

###添加“launch.json”文件配置

{
    // 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": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.out", // 將要進行調試的程序的路徑
            "args": [], // 程序調試時傳遞給程序的命令行參數,一般設爲空即可
            "stopAtEntry": false, // 設爲true時程序將暫停在程序入口處,一般設置爲false
            "cwd": "${workspaceFolder}", // 調試程序時的工作目錄,一般爲${workspaceRoot}即代碼所在目錄
            "environment": [],
            "externalConsole": true, // 調試時是否顯示控制檯窗口,一般設置爲true顯示控制檯
            "MIMode": "lldb",
            "preLaunchTask": "g++", // 調試會話開始前執行的任務,這裏爲認爲名字
            "setupCommands": [
                {
                    "description": "Enable pretty -printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

##編譯運行
快捷鍵“F5”。注意,“F5”只是調試快捷鍵,但是我們在Launch.json裏配置了,調試會話開始前執行g++的tasks.json的任務自動編譯了,手動編譯快捷鍵“⇧⌘B”。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章