Mac下VScode配置C/C++调试(debug)环境的有效方法

本文默认你使用的是CodeRunner来运行C++,并且能成功在VScode上运行C++程序

配置C++调试

安装C/C++ extension,添加配置 C/C++: (lldb) Launch
在这里插入图片描述

配置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": [
        {
            "preLaunchTask": "build c++",
            "name": "Launch C++",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true, // 在终端调试
            "MIMode": "lldb",
        },
        {
            "name": "Python",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "${config:python.pythonPath}",
            "program": "${file}",
            "cwd": "${workspaceRoot}",
            "env": {
                "PYTHONPATH": "${workspaceRoot}"
            },
            "envFile": "${workspaceRoot}/.env"
        }
    ]
}

externalConsole:设置为 true 是用mac自带终端来运行程序(凑合着还能用)

我根据下面的回答,把VScode默认终端改成了iterm2,但是调试是打开的还是mac自带的终端(暂时不知道怎么解决)
Change default terminal app in Visual Studio Code on Mac

配置task.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build c++",
            "type": "shell",
            "command": "g++",
            "args": [
                "${file}",
                "-std=c++17",
                "-o",
                "${workspaceFolder}/a.out",
                "-g"
            ],
            "group": "build",
            "presentation": {
                "echo": true,
                "focus": false,
                "panel":"shared",
                "showReuseMessage": true,
                "clear": false,
                "reveal": "silent"
            },
},
    ]
    
}

配置好这两个文件后,会保存在当前workplace目录中的.vscode文件
(ps:你配置好的debug环境只对当前工作区有效)
在这里插入图片描述
然后就可以打开调试界面进行调试了(快捷方式:命令面板输入Debug Launcher: Auto)
在这里插入图片描述

调试continue、step over、step into、step out

在这里插入图片描述

Continue – An action to take in the debugger that will continue execution until the next breakpoint is reached or the program exits.

Step over – An action to take in the debugger that will step over a given line. If the line contains a function the function will be executed and the result returned without debugging each line.

Step into – An action to take in the debugger. If the line does not contain a function it behaves the same as “step over” but if it does the debugger will enter the called function and continue line-by-line debugging there.

Step out – An action to take in the debugger that returns to the line where the current function was called.

https://www.fourkitchens.com/blog/article/step-step-through-debugging/

其他问题

问题解决:cpptools high CPU

安装了C/C++ extension后,每次会很高的cpu消耗,如下图所示
在这里插入图片描述
强行退出这个cpptools进程后,可以解决问题

但是每次都要这样杀进程,未免太麻烦了

所以要么卸载C/C++ extension,无法调试,要么找到解决方法…

最终折腾了很久,我终于有了解决方案

在settings.json中添加如下设置即可
"C_Cpp.intelliSenseEngine": "Disabled"

问题解决:launch.json matches multiple schemas

VSC 安装 Google Chrome Debugger 后出现Matches multiple schemas when only one must validate node 错误的解决方法

问题解决:Terminal乱码

在这里插入图片描述
修改成Hack Nerd Font,即可
在这里插入图片描述
REFERENCES
[1] 使用 VSCode 在 Mac 上配置 C/C++ 调试环境
[2] Change default terminal app in Visual Studio Code on Mac
在这里插入图片描述

发布了311 篇原创文章 · 获赞 164 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/qq_43827595/article/details/105609852