在 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来修改变量值等

相关推荐
Crossoads13 分钟前
【数据结构】排序算法---桶排序
c语言·开发语言·数据结构·算法·排序算法
扎克begod18 分钟前
JAVA并发编程系列(9)CyclicBarrier循环屏障原理分析
java·开发语言·python
code bean26 分钟前
【C#基础】函数传参大总结
服务器·开发语言·c#
阳光阿盖尔36 分钟前
EasyExcel的基本使用——Java导入Excel数据
java·开发语言·excel
蔚一38 分钟前
Java设计模式—面向对象设计原则(三) -----> 依赖倒转原则DIP(完整详解,附有代码+案例)
java·开发语言·设计模式·intellij-idea·依赖倒置原则
liang899943 分钟前
SpringSecurity原理解析(七):权限校验流程
java·开发语言
LQS202044 分钟前
基于Python实现一个浪漫烟花秀
开发语言·python
QXH2000001 小时前
数据结构—单链表
c语言·开发语言·数据结构
梅如你1 小时前
python批量对遥感影像进行归一化与数据清洗
开发语言·python
imaima6661 小时前
数据结构----栈和队列
开发语言·数据结构