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

相关推荐
雪的季节7 分钟前
qt信号槽跨线程使用时候的坑
java·开发语言·qt
AI应用实战 | RE11 分钟前
011、向量数据库入门:Embeddings原理与ChromaDB实战
开发语言·数据库·langchain·php
chh56313 分钟前
C++--内存管理
java·c语言·c++·windows·学习·面试
Yungoal14 分钟前
C++ 标准模板库STL(Standard Template Library)
c++·哈希算法·散列表
我真不是小鱼16 分钟前
cpp刷题打卡记录27——无重复字符的最长子串 & 找到字符串中所有字母的异位词
数据结构·c++·算法·leetcode
一直不明飞行23 分钟前
C++:string,写法s.find(‘@‘) != s.end()是否有问题
开发语言·c++·算法
沐知全栈开发34 分钟前
C 预处理器
开发语言
daad77737 分钟前
WSL2_wifi驱动安装
开发语言·前端·javascript
超级大只老咪1 小时前
一维度前缀和解题通用模板(java)
java·开发语言·算法
无限进步_1 小时前
【C++】重载、重写和重定义的区别详解
c语言·开发语言·c++·ide·windows·git·github