Ubuntu 使用 VS Code Debugger 除錯教學

本篇 ShengYu 介紹在 Ubuntu 使用 Visual Studio Code(VS Code) 設定 C/C++ 除錯的環境,

以下 Ubuntu 使用 VS Code 設定除錯環境的內容大概分為

  • VS Code 除錯環境設定
  • VS Code 偵錯前先編譯
  • VS Code 開啟 make 專案
  • VS Code 開啟 cmake 專案
  • VS Code 錯誤排除

那我們開始吧!

VS Code 除錯環境設定

我的 VS Code 版本為 1.68.0,Ubuntu 版本為 16.04,
在新專案下按下 F5 時如果沒有 .vscode/launch.json 設定檔的話,會跳出一些選項讓你選擇,如圖中的 C++: clang++

編譯時,新專案下沒有 .vscode/tasks.json 設定檔的話,也會跳出選項讓你選擇,這邊是示範選擇使用 clang++,用 g++ 也可以,

會在資料夾下產生 .vscode/launch.json 設定如下,如果沒有的話自己手動新增也可以,

.vscode/launch.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
// 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": "clang - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"miDebuggerPath": "/usr/bin/lldb-mi"
}
]
}

編譯 C/C++ 時記得加上 -g 選項,按 F5 執行 Start Debugging 後,就成功了!

VS Code 偵錯前先編譯

VS Code 按下 F5 要先編譯再啟動偵錯的話,要在 launch.json 加上 preLaunchTask,如下範例,

.vscode/launch.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
// 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": "clang - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: clang build active file",
"miDebuggerPath": "/usr/bin/lldb-mi"
}
]
}

launch.json 的 "preLaunchTask": "C/C++: clang build active file" 是對應到 tasks.json 的 label 名稱,

.vscode/tasks.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/a.out"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

這樣按 F5 啟動偵錯前會先編譯,成功編譯才會啟動偵錯。

詳細的 .vscode 設定可以參考 https://github.com/shengyu7697/vscode-debugging/tree/master/ubuntu-lldb

其他參考
launch.json 參數說明
Visual Studio Code (VSCode) 之 C/C++ 调试配置详解
[VSCode] Visual Studio Code 執行 C++ (2) - IntelliSense + Building + Debugging

VS Code 開啟 make 專案

make 專案編譯時會使用 Makefile 來執行裡面的腳本,所以 .vscode/tasks.json 不能在使用 cppbuild type 要換成 shell 來執行 make 這個指令,

然後 command 換成 cd ${workspaceFolder}; /usr/bin/make,如下所示,

.vscode/tasks.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"tasks": [
{
"type": "shell",
"label": "C/C++: make build",
"command": "cd ${workspaceFolder}; /usr/bin/make",
"args": [],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "偵錯工具產生的工作。"
}
],
"version": "2.0.0"
}

這邊也可以參考 Stack Overflowgithub gist 兩篇的其他種做法。

VS Code 開啟 cmake 專案

cmake 專案在 generate 時,要使用 cmake -DCMAKE_BUILD_TYPE=Debug .. 給定編譯 Debug 版本,之後編譯出來的執行檔才有除錯的資訊,或者在 CMakeLists.txt 直接寫死是 Debug 版本,

CMakeLists.txt
1
set(CMAKE_BUILD_TYPE "Debug")

.vscode/tasks.json 這邊可以參考這篇的寫法,將 cmake generate 與 cmake build 分成兩個 task,再用 C/C++: cmake build 這個 task 將前兩者串起來,

.vscode/tasks.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{
"tasks": [
{
"label": "C/C++: cmake build",
"dependsOrder": "sequence",
"dependsOn":["cmake generate", "cmake build"]
},
{
"type": "shell",
"label": "cmake generate",
"command": "cd ${workspaceFolder}/build; /usr/bin/env cmake ..",
"args": []
},
{
"type": "shell",
"label": "cmake build",
"command": "cd ${workspaceFolder}/build; /usr/bin/env cmake --build .",
"args": [],

"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "偵錯工具產生的工作。"
}
],
"options": {
"cwd": "${workspaceRoot}/build"
},
"version": "2.0.0"
}

VS Code 錯誤排除

如果在除錯的過程中逐步執行發現跳不到原始碼遇到 Could not load source ... 'SourceRequest' not supported 這樣的錯誤訊息的話,可能就是找不到原始碼的路徑,可能就是因為執行檔跟原始碼擺放路徑不同,解決方式就是在 launch.json 使用 sourceFileMap 將正確的路徑對應好,再次啟動就可以正確找到了~~

以上就是 Ubuntu 使用 VS Code Debugger 除錯介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!

其他參考
Debug a C++ project in VS Code - Youtube

其它相關文章推薦
macOS 使用 VS Code Debugger 除錯教學
Visual Studio Code 常用快捷鍵
VS Code 新增 C++ 標頭檔路徑