You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac上VS Code使用GDB调试C代码失败的原因及解决方法

在VS Code中使用GDB调试C项目遇到的问题

我使用搭载macOS Catalina 10.15.7的MacBook,尝试在Visual Studio Code(VS Code)中调试C项目。最初尝试使用LLDB,曾成功过几次,但它仅会打开系统终端,且后续对其他代码失效。因此转而使用GDB,已完成安装并通过Keychain创建了所需证书。

了解到LLDB存在无法使用VS Code内部终端的限制,这是我选择GDB的原因。我将launch.json中的"externalConsole"设置改为true和false均无效果,理想状态是让GDB正常工作而非LLDB。我已查阅大量教程和指南,但仍未解决问题。

我已通过VS Code终端手动执行命令生成main.out文件:gcc -g main.c -o main.out


详细信息

待调试代码:Main.c

#include <stdio.h>

int main(){
    int a,b,c;

    printf("input a number: ");
    scanf("%d", &a);
    printf("input another number: ");
    scanf("%d", &b);
    c = a+b;
    printf(" %d + %d = %d\n", a,b,c);
    return 0;
}

版本信息

  • LLDB版本:lldb-1200.0.44.2,Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)
  • GDB版本:GNU gdb (GDB) 13.2

launch.json配置

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/main.out",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "miDebuggerPath": "/usr/local/bin/gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "build",
      "internalConsoleOptions": "openOnSessionStart"
    }
  ]
}

tasks.json配置

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build",
      "type": "shell",
      "command": "gcc",
      "args": [
        "-g",
        "${workspaceFolder}/main.c",
        "-o",
        "${workspaceFolder}/main.out"
      ],
      "group": "build"
    }
  ]
}

内容的提问来源于stack exchange,提问作者Toni Fernandez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 00:12:06