雖然linux環境下使用命令行編譯可以使用gdb調試,但是不能跟隨代碼一步一步走,很麻煩
但是vscode通過配置task.json和launch.json可以達到一步一跟的效果。
對於文件不多的項目可以使用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 hello world", // task的名字
"type": "shell",
"command": "gcc", //編譯命令
"args": [ //編譯參數列表
"-g", // 加上-g可以斷點調試
"2048.c",
"-o",
"2048",
"-lcurses"
]
}
]
}
其中args里面的參數就是你使用命令行模式里的參數,對照着網上抄就行。
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": "debug hello world", //名稱
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/2048", //當前目錄下編譯后的可執行文件
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}", //表示當前目錄
"environment": [],
"externalConsole": false, // 在vscode自帶的終端中運行,不打開外部終端
"MIMode": "gdb", //用gdb來debug
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build hello world" //在執行debug hello world前,先執行build hello world這個task,看第4節
}
]
}
將 "program": "${workspaceFolder}/2048", 中的2018修改為task.json中你所生成的程序名。
應該有相關變量來代替這兩步的修改,可以適配所有的編譯調試命令。等我有時間去看一下官方文檔來操作一下。
現在就目前將就着用一下。