VsCode安装gdb调试并捕获printf输出


image.png

打开 MinGW Installation Manager 后在列表中找到mingw32-gdb(class为bin)的选项,右键点击Mark for Installation,然后点击左上角Installation菜单下的Apply Changes选项进行安装。

然后新建一个项目文件,此时会生成一个.vscode文件夹


image.png

如果不行,可以在文件夹内打开cpp文件然后点击setting按钮生成json还有.vscode文件夹。

图片.png

调试步骤:

用VSCODE打开调试程序所在文件夹,在文件夹中打开code文件。

图片.png

如果想让调试过程中也能输出printf:需要将该参数置为true。

图片.png

但是,奇怪的是VSCODE里面的堆栈地址跟打印出来的不一样。

图片.png

问了下大佬,因为&a打印的是string类对象的地址,而_M_p是char字符串指针的地址。

图片.png

ok,完美。


C++的launch.json

// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team        
// ${file}: the current opened file                     
// ${fileBasename}: the current opened file's basename 
// ${fileDirname}: the current opened file's dirname    
// ${fileExtname}: the current opened file's extension  
// ${cwd}: the current working directory of the spawned process

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "preLaunchTask": "build",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/Program Files (x86)/MinGW/bin/gdb.exe", // GDB的路径,注意替换成自己的路径
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }]
}

tasks.json:

{
    "version": "0.1.0",
    "command": "g++",
    "args": ["-g","${file}","-o","${file}.exe"],    // 编译命令参数
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "${workspaceRoot}"],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}

C的launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "preLaunchTask": "g++",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe", 
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }]
}

tasks.json

{
    "version": "2.0.0",
    "command": "g++",
    "args": ["-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}.exe"],    // 编译命令参数
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "${workspaceFolder}"],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    },
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "new", //这里shared表示共享,改成new之后每个进程创建新的端口
        "showReuseMessage": true,
        "clear": false
    }
}
  • 记得g++ xxx.cpp -o xxx -g-g要不然没法调试
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容