主要看下列两个文档即可,建议仔细阅读,二者结合起来看
在左侧Extensions中搜索C/C++,安装即可
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-x64"
}
],
"version": 4
}
tasks.json是负责Run的。想要更详细的了解task,可参考官方文档
{
"tasks": [
{
"type": "cppbuild",
// 与launch.json中的preLaunchTask对应,这里要注意,默认生成出的label中带有中文,我这里修改为全英文
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang",
"args": [
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
launch.json是负责debug的。
{
// 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
// 所有属性:https://code.visualstudio.com/docs/editor/debugging#_launchjson-attributes
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: clang++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
// 使用外部终端
"externalConsole": true,
"MIMode": "lldb",
// 和tasks.json中的label对应
"preLaunchTask": "C/C++: clang++ build active file"
}
]
}
这里只说明两点,就是上述文件中带注释的部分,
launch.json的所有属性详解见官方文档User Guide > Debugging部分
在程序中打断点,并使用Debug模式启动,发现打开了Mac自动自带的终端,在debug模式下可以在终端输入,可以正常debug程序。
Stackoverflow上的问题:How to configure VS Code so it won’t close console after program run
在网上找了些答案,基本是以下几种
都不是我想要的,最终发现了评论里的一句话,
Place a breakpoint at the end of main
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "D:/MinGW/bin/gcc.exe",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x86"
}
],
"version": 4
}
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "D:\\MinGW\\bin\\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "C Debug",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
// 模式是GDB
"MIMode": "gdb",
"miDebuggerPath": "D:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file",
}
]
}
So, I would use LLDB while using Clang, use GDB while using GCC compiler as the good combination or pair because LLDB is based on LLVM, whereas GDB is the GNU debugger