在Vscode中使用gdb调试程序

详情参考官方文档

set(CMAKE_BUILD_TYPE Debug)		# 首先要在CMakeList.txt中设置dubug模式

1 创建launch.json文件

在这里插入图片描述

l a u n c h . j s o n \textcolor{red}{launch.json} launch.json

  调试多个程序和调试一个程序是一样的道理,只需要后面把两个文件compounds

{
    // 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": "main",    // 使用哪一种调试器,gdb为gcc自带的
            "type": "cppdbg",          // 程序类型cpp
            "request": "launch",
            "preLaunchTask": "Build",  // 保证该名字与task.json中的最后生成的文件相同  即:编译文件存放的位置
            "program": "${workspaceFolder}/bin/main_outcar",  // 生成的可执行文件的路径,                  命令形式 "program": "${workspaceFolder}/your_executable",
            
            "args": [ "/home/xxx"],   //终端输入参数,不包括可执行文件  形式 "args": ["arg1", "arg2"]
            
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}", //当前文件s路径
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",  // gdb的路径,默认
            "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
                }
            ]
        },
        {
            "name": "test",    // 使用哪一种调试器,gdb为gcc自带的
            "type": "cppdbg",          // 程序类型cpp
            "request": "launch",
            "preLaunchTask": "Build",  // 保证该名字与task.json中的最后生成的文件相同  即:编译文件存放的位置
            "program": "${workspaceFolder}/bin/function_text",  // 生成的可执行文件的路径,                  命令形式 "program": "${workspaceFolder}/your_executable",
            
            "args": [ ],   //终端输入参数,不包括可执行文件  形式 "args": ["arg1", "arg2"]
            
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}", //当前文件s路径
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",  // gdb的路径,默认
            "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
                }
            ]
        }
    ],
    "compounds": [
        {
          "name": "main/test",
          "configurations": ["main", "test"],
          "stopAll": true
        }
      ]
}  

2 创建tasks.json文件

t a s k s . j s o n \textcolor {red} {tasks.json} tasks.json

  这个文件是通用的,直接复制粘贴即可。这个文件主要为了编译代码,所以如果不对代码修改,其实也可以不加这个文件。

{
    "options": {
        "cwd": "${workspaceFolder}/build"
    },
    "tasks": [
        {
            "type": "shell",      // 相当于对整个文件进行编译的步骤     此处代表cmake ..
            "label": "cmake",
            "command": "cmake",
            "args": [
                ".."
            ]
        },
        {
            "label": "make",     // 此处代表make
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "command": "make",
            "args": []
        },
        {
            "label": "Build",   		// label表示编译的文件存放的位置,要求与launch.json中的preLaunchTask相同
            "dependsOrder": "sequence",   
            "dependsOn":["cmake","make"]   // 此处表示联合两条指令
        }
    ],
    "version": "2.0.0"
}

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值