在 VS Code 中调试 C++ 项目

选择调试器环境

从预定义的调试配置中进行选择,生成预定义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": [
        
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            //设置为活动文件夹和活动文件名
            // "program": "${workspaceFolder}/leetcode100/06矩阵/a.out",  
            "program": "${fileDirname}/${fileBasenameNoExtension}",  

            //启动程序时传递给程序的命令行参数数组 
            "args": [],
            //默认不会添加任何断点,如果希望调试器在开始调试时停止在main方法,将stopAtEntry设置为true
            "stopAtEntry": true,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            //指示调试器将连接到gdb或lldb
            "MIMode": "gdb",
            //调试器的路径,如果没有指定,将在计算机路径变量中搜索调试器,取决于MIMODE
            "miDebuggerPath": "/usr/bin/gdb",
            //指定要传递给调试器的附加参数
            "miDebuggerArgs": "",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }

    ]
}

举例

复制代码
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    vector<string> msg {"hello", "world", "from", "vs code"};

    for (const string& word : msg) {
        cout << word << " "; 
    }
    cout << endl;
}

(base) daichang@daichang:~/Desktop/Algorithm-training/gdb$ g++ helloworld.cc -o helloworld

开始调试,使用键盘快捷键F5

在Debug Console 显示调试器已启动并正在运行的输出,在最后一个cout语句完成之前,调试控制台不会出现任何输出。因为需要换行符来刷新缓冲区

可以使用变量缓冲区中的set alue来修改变量值等

相关推荐
山登绝顶我为峰 3(^v^)330 分钟前
如何录制带备注的演示文稿(LaTex Beamer + Pympress)
c++·线性代数·算法·计算机·密码学·音视频·latex
Python×CATIA工业智造3 小时前
Frida RPC高级应用:动态模拟执行Android so文件实战指南
开发语言·python·pycharm
十五年专注C++开发3 小时前
CMake基础:条件判断详解
c++·跨平台·cmake·自动化编译
我叫小白菜4 小时前
【Java_EE】单例模式、阻塞队列、线程池、定时器
java·开发语言
狐凄4 小时前
Python实例题:基于 Python 的简单聊天机器人
开发语言·python
weixin_446122465 小时前
JAVA内存区域划分
java·开发语言·redis
悦悦子a啊5 小时前
Python之--基本知识
开发语言·前端·python
QuantumStack6 小时前
【C++ 真题】P1104 生日
开发语言·c++·算法
天若有情6736 小时前
01_软件卓越之道:功能性与需求满足
c++·软件工程·软件
whoarethenext6 小时前
使用 C++/OpenCV 和 MFCC 构建双重认证智能门禁系统
开发语言·c++·opencv·mfcc