vscode配置调试C/C++程序——linux环境(命令行编译)

虽然linux环境下使用命令行编译可以使用gdb调试,但是不能跟随代码一步一步走,很麻烦

但是vscode通过配置task.json和launch.json可以达到一步一跟的效果。

对于文件不多的项目可以使用vscode模拟命令行编译效果来调试

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 hello world",     // task的名字
            "type": "shell",   
            "command": "gcc",    //编译命令
            "args": [    //编译参数列表
                "-g", // 加上-g可以断点调试
                "2048.c",
                "-o",
                "2048",
                "-lcurses"
            ]
        }
    ]
}
复制代码

其中args里面的参数就是你使用命令行模式里的参数,对照着网上抄就行。

 

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": "debug hello world",    //名称
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/2048",    //当前目录下编译后的可执行文件
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",    //表示当前目录
            "environment": [],
            "externalConsole": false, // 在vscode自带的终端中运行,不打开外部终端
            "MIMode": "gdb",    //用gdb来debug
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build hello world"  //在执行debug hello world前,先执行build hello world这个task,看第4节
        }
    ]
}
复制代码

 

将 "program": "${workspaceFolder}/2048", 中的2018修改为task.json中你所生成的程序名。

 

应该有相关变量来代替这两步的修改,可以适配所有的编译调试命令。等我有时间去看一下官方文档来操作一下。

现在就目前将就着用一下。

posted @   王清河  阅读(2796)  评论(1编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示