How to?

c-cmake-debug

Jan 15, 2024
softwares-and-toolsvscodedebug
2 Minutes
308 Words
This article was last updated on May 27, 2026 and some of the information may no longer be applicable due to the passage of time.

原理是这样的,你编译出来的可执行文件只要是编译的时候开了 Debug 的时候,是可以直接调试的。

所以你在 cmakelists.txt 加上:

1
set(CMAKE_BUILD_TYPE "Debug")

然后去 cmake + make 一下。

现在,用 vscode 命令面板唤出 debug 面板。

面板里有一个链接让你创建 launch.json。你点击会让你选择调试环境,你选 LLDB 他生成一个 json,然后这个 json 内容改成这样:

1
{
2
"version": "0.2.0",
3
"configurations": [
4
{
5
"type": "lldb",
6
"request": "launch",
7
"name": "Debug",
8
"program": "${workspaceFolder}/build/demo",
9
"args": [],
10
"cwd": "${workspaceFolder}"
11
}
12
]
13
}

这边 “program” 条目你改成要执行的文件。没错直接可执行文件就行了。

现在在源文件里面加一个断点,然后捏点击 debug 面板上方的播放按钮。他就会停在断点了。还会出现一个小长条工具栏,用来到下一行、跳转行等。一般你用 F10 逐过程,避免进入每个函数。

如果你要输入数据,就在面板的“终端”里面输入。

Problem

  • 点击创建 launch.json 的时候可能会卡。

这可能是因为某些其他语言的 vscode debugger 在试图更新自己的配置。建议手动创建 launch.json 或者等一等就会有。

Article title:c-cmake-debug
Article author:Julyfun
Release time:Jan 15, 2024
Copyright 2026
Sitemap