win11 + wsl + vscode + cpptools调试

安装c/c++插件

为了使用调试功能,重新安装了c/c++拓展插件,cpptools 只当“调试器前端”用,代码提示/跳转/诊断仍然全部交给 clangd,同时又为了不影响clang等拓展的功能,因此需要禁用一些功能

windows

  1. 窗口里按 Ctrl+Shift+P
  2. 输入并选择:Preferences: Open User Settings (JSON)
  3. 确认打开的路径是:/C:/Users/dky/AppData/Roaming/Code/User/settings.json
  4. 把这三行加进去并保存:
1
2
3
"C_Cpp.intelliSenseEngine": "disabled",
"C_Cpp.autocomplete": "disabled",
"C_Cpp.errorSquiggles": "disabled"

这是“windows端全局设置”。

wsl

  1. WSL 窗口里按 Ctrl+Shift+P
  2. 输入并选择:Preferences: Open Remote Settings (JSON)
  3. 或者直接打开:code ~/.vscode-server/data/Machine/settings.json
  4. 确认打开的路径是:/home/dky/.vscode-server/data/Machine/settings.json
  5. 把这三行加进去并保存:
1
2
3
"C_Cpp.intelliSenseEngine": "disabled",
"C_Cpp.autocomplete": "disabled",
"C_Cpp.errorSquiggles": "disabled"

这就是 Remote Settings ,“WSL 端全局设置”。

项目工作区配置

.vscode配置

.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
35
36
37
38
39
{
"version": "2.0.0",
"tasks": [
{
"label": "build-gtk-debug",
"type": "shell",
"command": "make",
"args": ["TARGET=gtk", "DBG=1", "-j"],
"options": { "cwd": "${workspaceFolder}" },
"problemMatcher": ["$gcc"],
"group": { "kind": "build", "isDefault": true }
},
{
"label": "build-cli-debug",
"type": "shell",
"command": "make",
"args": ["TARGET=cli", "DBG=1", "-j"],
"options": { "cwd": "${workspaceFolder}" },
"problemMatcher": ["$gcc"]
},
{
"label": "build-sdl-debug",
"type": "shell",
"command": "make",
"args": ["TARGET=sdl", "DBG=1", "-j"],
"options": { "cwd": "${workspaceFolder}" },
"problemMatcher": ["$gcc"]
},
{
"label": "build-gl-debug",
"type": "shell",
"command": "make",
"args": ["TARGET=gl", "DBG=1", "-j"],
"options": { "cwd": "${workspaceFolder}" },
"problemMatcher": ["$gcc"]
}
]
}

ctrl shift b运行该task

.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
30
31
32
33
34
35
36
37
38
39
{
"version": "2.0.0",
"tasks": [
{
"label": "build-gtk-debug",
"type": "shell",
"command": "make",
"args": ["TARGET=gtk", "DBG=1", "-j"],
"options": { "cwd": "${workspaceFolder}" },
"problemMatcher": ["$gcc"],
"group": { "kind": "build", "isDefault": true }
},
{
"label": "build-cli-debug",
"type": "shell",
"command": "make",
"args": ["TARGET=cli", "DBG=1", "-j"],
"options": { "cwd": "${workspaceFolder}" },
"problemMatcher": ["$gcc"]
},
{
"label": "build-sdl-debug",
"type": "shell",
"command": "make",
"args": ["TARGET=sdl", "DBG=1", "-j"],
"options": { "cwd": "${workspaceFolder}" },
"problemMatcher": ["$gcc"]
},
{
"label": "build-gl-debug",
"type": "shell",
"command": "make",
"args": ["TARGET=gl", "DBG=1", "-j"],
"options": { "cwd": "${workspaceFolder}" },
"problemMatcher": ["$gcc"]
}
]
}

fn f5执行该launch

如果想每次调试都重新编译,把command改成bash一条命令:

1
2
3
"command": "bash",
"args": ["-lc", "make clean && make TARGET=gtk DBG=1 -j"]

保险起见,在工作区也彻底禁用cpptools的非调试功能,避免和clangd冲突

.vscode/settings.json配置

1
2
3
4
5
6
7
8
9
10
11
12
13
{
// cpptools:关闭 IntelliSense/补全/红波浪/自动分析
"C_Cpp.intelliSenseEngine": "disabled",
"C_Cpp.autocomplete": "disabled",
"C_Cpp.errorSquiggles": "disabled",
"C_Cpp.codeAnalysis.runAutomatically": false,
"C_Cpp.workspaceParsingPriority": "low",

// 仍然用 clangd 做 C 语言格式化/提示(你已经在用的话可保留)
"[c]": { "editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd" },
"[cpp]": { "editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd" }
}

makefile配置

1
2
3
4
5
# gdb 调试选项
DBG ?= 0
ifeq ($(DBG),1)
CFLAGS += -Og -g3
endif

调试

大致步骤就是:

  1. ctrl shift b编译项目

  2. vscode打断点

  3. fn f5调试