1. 程式人生 > 其它 >VSCode配置luanch和task

VSCode配置luanch和task

技術標籤:C++

1、編輯launch.json

{
    // 使用 IntelliSense 瞭解相關屬性。 
    // 懸停以檢視現有屬性的描述。
    // 欲瞭解更多資訊,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations":[

        {
            // 配置名稱,將會在啟動配置的下拉選單中顯示
            "name": "g++.exe build and debug active file",

            // 配置型別,這裡只能為cppdbg
            "type": "cppdbg",

            // 請求配置型別,可以為launch(啟動)或attach(附加)
            "request": "launch",

            // 將要進行除錯的程式的路徑 
            "program": "C:\\Windows\\System32\\cmd.exe",

            // 程式除錯時傳遞給程式的命令列引數,一般情況設為空
            "args": ["/C","${fileDirname}\\${fileBasenameNoExtension}.exe","&","pause"],

            // 設為true時程式將暫停在程式入口處,一般設定為false 
            "stopAtEntry": false,

            // 除錯程式時的工作目錄,一般為${workspaceRoot}即程式碼所在目錄
            "cwd": "${workspaceRoot}",

            //(環境變數?)英文翻譯是這樣的
            "environment": [],

            // 除錯時是否顯示控制檯視窗,一般設定為true顯示控制檯
            "externalConsole": true,

            //指定連線的偵錯程式,可以為gdb或lldb。但目前lldb在windows下沒有預編譯好的版本。
            "MIMode": "gdb",

            //偵錯程式路徑,Windows下字尾不能省略,Linux下則去掉
            "miDebuggerPath": "D:\\SoundCode\\MinGW\\bin\\gdb.exe",

            "setupCommands": [
                {
                    "description": "為 gdb 啟用整齊列印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
             // // 除錯會話開始前執行的任務,一般為編譯程式。與tasks.json的label相對應
            "preLaunchTask": "c/cpp task"
        },

        {
            "name": "debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\SoundCode\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": false
                }
            ],
            "preLaunchTask": "c/cpp task"
        }
        ]
       }

1、先將program中的引數移動到args,然後修改成Cmd路徑: "C:\\Windows\\System32\\cmd.exe",

這裡Cmd路徑在WINDOWS系統是單反斜槓,所以在這需要加多一個反斜槓進行轉義

2、然後在args中新增:"/C","&","pause"

其中添加了pause之後就不需要每次printf後面加上system("pause");或者getchar()語句.解決了終端閃退問題

3、將externalConsole中的引數改為:"true

"

4、將preLaunchTask中的引數改為:"c/cpp task"

5、如果出現程式不能除錯的問題,將下面的program引數改成:"${fileDirname}/${fileBasenameNoExtension}",

2、編輯task.json

{
    "tasks": [
        {
            "type": "shell",
            "label": "c/cpp task",
            "command": "D:\\SoundCode\\MinGW\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:\\SoundCode\\MinGW\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        },
        {
            "type": "shell",
            "label": "c/cpp task",
            "command": "D:\\SoundCode\\MinGW\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:\\SoundCode\\MinGW\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

1、將label引數修改成:"c/cpptask",

此處有倆出label,注意

2、將type引數改成:"shell",

配置好luanch和task差不多大功告成,關於除錯、閃退問題差不多都在上面解決了。

如果出現下圖中:在系統中找不到指定路徑,一定是專案檔案出現中文名字,改成英文即可