20220111c_debug

Jan 11, 2022

Windows c debug

使用 windows vscode 进行c debug:

下载C/C++ 插件, 首先先生成执行文件: gcc main.c add.c -ledit -g -o add

此时会生成add.exe 文件。

然后项目根目录生成.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
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"program": "${fileDirname}/add",
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为gdb启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailres": true
}
]
}
]
}

按F5 即可启动。

当前办法是每次构建需要自己先执行下生成命令,然后按F5. 之后优化。

Mac c debug

操作如同上面, 不过可以增加一个Task简约操作:

tasks.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc 生成活动文件",
"command": "/usr/bin/gcc",
"args": ["-g", "${file}", "mpc.c", "-ledit", "-o", "${fileDirname}/ss"],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "编译器: /usr/bin/gcc"
}
]
}

此时 Shift+Command+p 点击 Tasks: Run Task. 这样会生成如手工生成的文件: gcc mpc.c ss.c -g -ledit -o ss

然后附以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
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Attach",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"program": "${fileDirname}/ss",
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"setupCommands": [
{
"description": "为gdb启用整起打印",
"text": "-enable-pretty-printing",
"ignoreFailres": true
}
]
}
]
}

以上mac就能初步享受断点开发了。操作繁琐一些,先使用再看看。

更新vscode 的cpp插件出现Unable to start debugging. LLDB exited unexpectedly with exit code 134 (0x86).报错。原始是cpp插件不是正式版,点击切换为正式发行版即正常。