VSCode调试C++代码报错:LLDB执行-exec-run失败,无法启动调试
问题描述
我正在学习面向游戏开发的C++入门课程,上周调试功能还能正常运行,但近期运行调试时突然报错。
报错信息
Warning: Debuggee TargetArchitecture not detected, assuming x86_64. ERROR: Unable to start debugging. Unexpected LLDB output from command "-exec-run". process exited with status -1 (attach failed (Not allowed to attach to process. Look in the console messages (Console.app), near the debugserver entries, when the attach failed. The subsystem that denied the attach permission will likely have logged an informative message about why it was denied.)) The program '/Users/bcarson/Documents/GameDev/dapper-dasher/main' has exited with code 42 (0x0000002a).
我仅新建了一个项目,未做其他修改,但之前的项目也无法运行。搜索相关资料后,尝试安装CodeLLDB扩展并修改launch.json配置,将程序路径设为"${workspaceFolder}/main.cpp",但问题仍未解决。
设备信息
macOS Monterey Version 12.6.1 MacBook Pro (13-inch, 2019) Processor 1.4 GHz Quad-Core Intel Core i5
VSCode版本信息
Version: 1.74.1 Commit: 1ad8d514439d5077d2b0b7ee64d2ce82a9308e5a Date: 2022-12-14T10:33:40.793Z Electron: 19.1.8 Chromium: 102.0.5005.167 Node.js: 16.14.2 V8: 10.2.154.15-electron.0 OS: Darwin x64 21.6.0 Sandboxed: Yes
main.cpp代码
#include "raylib.h" int main() { //window dimensions const int windowWidth = 1300; const int windowHeight = 1300; //initialize the window InitWindow(windowWidth, windowHeight, "That Dasher Sure is Dapper!"); SetTargetFPS(60); while (!WindowShouldClose()) { //start drawing BeginDrawing(); ClearBackground(WHITE); //stop drawing EndDrawing(); } CloseWindow(); }
launch.json配置
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "type": "lldb", "request": "launch", "name": "Launch", "program": "${workspaceFolder}/main.cpp", "args": [], "cwd": "${workspaceFolder}" }, { "name": "Debug", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": false } ], "windows": { "miDebuggerPath": "C:/raylib/w64devkit/bin/gdb.exe", }, "osx": { "MIMode": "lldb" }, "linux": { "miDebuggerPath": "/usr/bin/gdb", }, "preLaunchTask": "build debug" }, { "name": "Run", "type": "cppdbg", "request": "launch", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "program": "${workspaceFolder}/${fileBasenameNoExtension}", "MIMode": "gdb", "windows": { "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe", "miDebuggerPath": "C:/raylib/w64devkit/bin/gdb.exe" }, "osx": { "MIMode": "lldb" }, "linux": { "miDebuggerPath": "/usr/bin/gdb", }, "preLaunchTask": "build release", } ] }
解决方案
1. 修正launch.json程序路径错误
调试需要指向编译后的可执行文件,而非源文件。修改CodeLLDB配置项的program字段:
{ "type": "lldb", "request": "launch", "name": "Launch", "program": "${workspaceFolder}/main", // 替换为编译生成的可执行文件路径(macOS下无后缀) "args": [], "cwd": "${workspaceFolder}" }
2. 解决macOS调试权限限制
针对Not allowed to attach to process报错:
- 打开「系统设置」→「隐私与安全性」→「完全磁盘访问权限」,勾选VSCode和
/Library/Developer/CommandLineTools/usr/bin/debugserver的权限。 - 重启Mac后重新打开VSCode尝试调试。
- 若仍报错,打开「Console.app」搜索
debugserver,查看具体拒绝原因后针对性处理。
3. 验证编译任务有效性
检查项目根目录的.vscode/tasks.json,确保build debug和build release任务配置正确,能正常生成可执行文件。也可手动在终端执行编译命令测试:
g++ main.cpp -o main -lraylib
编译完成后运行./main,确认程序能正常启动。
4. 简化配置排除干扰
暂时禁用多余的调试配置,只保留修改后的CodeLLDB配置,避免冲突。同时更新CodeLLDB扩展到最新版本,重启VSCode后重试。
内容的提问来源于stack exchange,提问作者billyc
相关产品推荐
相关产品推荐

