[英]How to setup C++ with debugger on mac in visual studio code
我正在尝试设置 Visual Studio 代码以在 macOS 上使用 g++ 编译器构建/运行和调试 c++ 文件。 但是,当我构建或调试代码时,我可以看到输出文件已创建并且我也可以运行它但是我无法在 vscode 中调试它,因为调试会产生奇怪的行为。
到目前为止,我已经能够编写构建 .cpp 文件并执行它的任务。 输出进入 vscode 的终端。 但是,当我尝试调试时,会打开一个新终端并且没有命中断点。
这是tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with g++",
"type": "shell",
"command": "g++",
"args": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-std=c++11",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "run",
"type": "shell",
"command": "cd ${fileDirname}/ && ./${fileBasenameNoExtension}",
"dependsOn": ["Build with g++"]
}
]
}
这是我的launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"showDisplayString": false,
"environment": [],
"externalConsole": true,
"internalConsoleOptions": "openOnSessionStart",
"MIMode": "lldb",
"logging": {
"moduleLoad": false,
"programOutput": true,
"trace": false
},
"preLaunchTask": "Build with g++",
"osx": {
"MIMode": "lldb"
}
}
]
}
我希望当我点击调试或按 f5 时,vscode 中的终端应该运行并且我能够调试程序。
正如@ alan-birtles指出的那样,添加-g将是解决方案。 另外我在这里发现,由于lldb不支持,因此无法将vscode的集成终端用于c ++。
这是最终配置。
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"showDisplayString": false,
"environment": [],
"externalConsole": true,
"internalConsoleOptions": "openOnSessionStart",
"MIMode": "lldb",
"logging": {
"moduleLoad": false,
"programOutput": true,
"trace": false
},
"preLaunchTask": "Build with g++",
"osx": {
"MIMode": "lldb"
}
}
]
}
task.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with g++",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-Wall",
"-Wextra",
"-Wpedantic",
"-std=c++11",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "run",
"type": "shell",
"command": "cd ${fileDirname}/ && ./${fileBasenameNoExtension}",
"dependsOn": ["Build with g++"]
}
]
}
因为你的VSCode Debugger Extension
有 bug。
在 mac 上使用 VSCode 成功设置 C++ Debug:我使用clang/gcc
作为编译器, Code-Runner
(VSCode Extension)作为运行器, C/C++ Clang Command Adopter
(VSCode Extension)作为提供静态检测, CodeLLDB
(VSCode Extension)进行调试C++。 请勿下载Microsoft C/C++
(标识符: ms-vscode.cpptools
,用于 C/C++ IntelliSense、调试和代码浏览。Microsoft 官方出品的扩展程序),因为这个 CPPtools 和那个 CodeLLDb 冲突。
为避免任何问题,请不要下载任何 VSCode 扩展。 这是我在 mac 上使用 VSCode 调试 C++ 的配置: VSCode v1.63.2
;
Code Runner v0.11.6
(作者:韩君)
C/C++ Clang Command Adapter v0.2.4
(作者:Yasuaki MITANI)
CodeLLDB v1.4.5
(作者:Vadim Chugunov)
Clang v9.0.0
gcc v4.2.1
Intel CPU
macOS 10.12
配置成功详情参考这里:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.