1. 程式人生 > 其它 >VScode tasks.json和launch.json

VScode tasks.json和launch.json

技術標籤:——VS、VSCodevscode

按 F5 會建立預設的。

給 vscode 的執行任務 tasks.json 新增快捷鍵繫結功能 預設的是 ctrl+shift+b 來選擇任務,如果配置了預設任務則會直接執行,否則要手動選擇。

// tasks.json
{
    // https://code.visualstudio.com/docs/editor/tasks
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build",  // 任務的名字叫Build,注意是大小寫區分的,等會在launch中呼叫這個名字
            "type": "shell",  // 任務執行的是shell命令,也可以是
            "command": "g++", // 命令是g++
            "args": [
                "'-Wall'",
                "'-std=c++17'",  //使用c++17標準編譯
                "'${file}'", //當前檔名
                "-o", //物件名,不進行編譯優化
                "'${fileBasenameNoExtension}.exe'",  //當前檔名(去掉副檔名)
            ],
          // 所以以上部分,就是在shell中執行(假設檔名為filename.cpp)
          // g++ filename.cpp -o filename.exe
            "group": { 
                "kind": "build",
                "isDefault": true   
                // 任務分組,因為是tasks而不是task,意味著可以連著執行很多工
                // 在build組的任務們,可以通過在Command Palette(F1) 輸入run build task來執行
                // 當然,如果任務分組是test,你就可以用run test task來執行 
            },
            "problemMatcher": [
                "$gcc" // 使用gcc捕獲錯誤
            ],
        }
    ]
}
// launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch", //這個應該是F1中出現的名字
            "preLaunchTask": "Build",  //在launch之前執行的任務名,這個名字一定要跟tasks.json中的任務名字大小寫一致
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe", //需要執行的是當前開啟檔案的目錄中,名字和當前檔案相同,但副檔名為exe的程式
            "args": [],
            "stopAtEntry": false, // 選為true則會在開啟控制檯後停滯,暫時不執行程式
            "cwd": "${workspaceFolder}", // 當前工作路徑:當前檔案所在的工作空間
            "environment": [],
            "externalConsole": true,  // 是否使用外部控制檯,選false的話,我的vscode會出現錯誤
            "MIMode": "gdb",
            "miDebuggerPath": "c:/MinGW/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }]
}

.launch.json

所需要除錯的檔案的路徑、除錯時的CWD(工作路徑)、偵錯程式的路徑及一些除錯引數(程式啟動引數等);

{
    // 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": [
        {
            "cwd": "${workspaceRoot}",
            "executable": "./bin/HAL_Test.elf",
            "name": "stm32 Debug",
            "request": "launch",
            "type": "cortex-debug",
            "servertype": "stutil",
            "device": "STM32F407ZG",
            "preLaunchTask": "編譯並下載",
            "postDebugTask": "復位裝置"
        }
    ]
}

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": "編譯",
            "type": "shell",
            "command": "make -j6",
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "編譯並下載",
            "type": "shell",
            "command": "make -j6 && make update",
            "problemMatcher": []
        },
        {
            "label": "重新編譯",
            "type": "shell",
            "command": "make clean && make -j6",
            "problemMatcher": []
        },
        {
            "label": "復位裝置",
            "type": "shell",
            "command": "STM32_Programmer_CLI -c port=SWD -hardRst",
            "problemMatcher": []
        }
    ]
}