ubuntu的vscode debug配置主要是配置launch.json和task.json兩個檔案,具體步驟如下:
首先在ubuntu上安裝gcc g++ gdb
sudo apt-get install gcc g++ gdb
然後在vscode商店下載下傳相關插件
c/c++
cmake
配置launch.json
{
// 使用 IntelliSense 了解相關屬性。
// 懸停以檢視現有屬性的描述。
// 欲了解更多資訊,請通路: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file", // 配置名稱,将會在啟動配置的下拉菜單中顯示
"type": "cppdbg", // 配置類型,這裡隻能為cppdbg
"request": "launch", // 請求配置類型,可以為launch(啟動)或attach(附加)
"program": "${workspaceFolder}/our/ch5/imageBasics/imageBasics", // 将要進行調試的程式的路徑
"args": ["/home/hjk/workspace/slam_learn/slambook2/ch5/imageBasics/ubuntu.png"], // 程式調試時傳遞給程式的指令行參數
"stopAtEntry": false, // 設為true時程式将暫停在程式入口處
"cwd": "${workspaceFolder}/our/ch5/imageBasics/", // 調試程式時的工作目錄
"environment": [], // (環境變量)
"externalConsole": false, // 調試時是否顯示控制台視窗,一般設定為true顯示控制台
"MIMode": "gdb", // 指定連接配接的調試器,可以為gdb或lldb。
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build", // 調試會話開始前執行的任務,一般為編譯程式。與tasks.json的label相對應
}
]
}
配置task.json
{
"tasks": [
{
"type": "shell", // 可以為shell或process,前者相當于先打開shell再輸入指令,後者是直接運作指令
"label": "build", // 任務名稱,與launch.json的preLaunchTask相對應
"command": "cd ${workspaceFolder}/our/ch5/imageBasics/build;cmake ..;make", // 要使用的編譯器
"args": [
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
修改CMakeLists.txt
如果使用cmake進行編譯,需要對CMakeLists檔案進行修改,隻需添加以下語句。
set(CMAKE_BUILD_TYPE "Debug")
debug調試
完成上述配置後,隻需要打上斷點,按F5進行調試。