使用gdb来调试c++程序, vscode的launch.json应该怎么写
时间: 2023-04-09 16:02:34 浏览: 158
你可以这样回答:“你可以在 launch.json 文件中添加以下配置来使用 gdb 调试 C 程序:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}”
相关问题
vscode launch.json配置c
在 VSCode 中,可以使用 launch.json 来配置 C 程序的调试选项。首先,打开调试面板 (Ctrl + Shift + D),然后点击第一个按钮,选择 C/C++ (GDB/LLDB)。这将会在工作区中创建一个 launch.json 文件。
在 launch.json 中,可以配置各种调试选项,如程序路径、编译器、调试器等。具体配置方法可以参考 VSCode 的官方文档。
示例配置:
```
{
"version": "0.2.0",
"configurations": [
{
"name": "gdb",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/main.c",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "gcc"
}
]
}
```
这样,你就可以在VSCode中调试你的C程序了。
Mac vscode launch.json配置
### 如何在 Mac 上配置 VSCode 的 `launch.json` 文件
#### 创建和编辑 `launch.json`
为了能够在 macOS 上利用 Visual Studio Code (VS Code) 进行有效的调试,需要正确地设置 `launch.json` 文件。此文件位于项目根目录下的 `.vscode` 文件夹内。
如果该文件不存在,则可以通过点击左侧活动栏中的调试图标,随后选择顶部菜单的 "create a launch.json file" 来自动生成模板[^1]。
#### 配置 Python 调试器
对于Python程序,在`.vscode/launch.json` 中的一个基本配置可能看起来像这样:
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: 当前文件",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
```
这段 JSON 设置允许用户通过按下快捷键或点击图形界面里的绿色箭头来启动当前打开脚本的调试会话[^2]。
#### C/C++ 调试配置实例
当涉及到C/C++开发时,除了 `launch.json` 外还需要其他几个辅助文件如 `tasks.json`, 和 `c_cpp_properties.json` 来定义编译任务以及 IntelliSense 的路径和其他选项。一个简单的用于GCC/G++ 编译器链的 `launch.json` 可能如下所示:
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb", // 使用 lldb 作为调试工具
"preLaunchTask": "gcc build active file", // 构建之前执行的任务名称
"miDebuggerPath": "/usr/bin/lldb-mi", // 如果不是默认位置则指定LLDB-MI路径
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"internalConsoleOptions": "openOnSessionStart"
}
]
}
```
上述配置假设已经有一个预构建任务被命名为 `"gcc build active file"` 并且会在实际运行之前自动触发它以确保最新的二进制可执行文件总是可用[^3]。
阅读全文
相关推荐















