vscode中的调试与编译

  • 调试
    1. 如果要启动调试, 需要设置启动配置文件–launch.json。选择调试环境, vscode将在.vscode目录下生成一个launch.json的配置文件。如下图所示:
      这里写图片描述
      2.编辑launch.json文件
{
    //${workspaceRoot} the path of the folder opened in VS Code(VSCode中打开文件夹的路径)
    //${workspaceRootFolderName} (VSCode中打开文件夹的路径, 但不包含"/")
    //${file} the current opened file(当前打开的文件)
    //${relativeFile} (当前打开的文件,相对于workspaceRoot)
    //${fileBasename} (当前打开文件的文件名, 不含扩展名)
    //${fileDirname} (当前打开文件的目录名)
    //${fileExtname} (当前打开文件的扩展名)
    "version": "0.2.0",
    "configurations": [    
        {
            "name": "(gdb) Launch",// 配置名称,将会在启动配置的下拉菜单中显示
            "type": "cppdbg",// 配置类型,这里只能为cppdbg
            "request": "launch",// 请求配置类型,可以为launch(启动)或attach(附加)
            "program": "${workspaceFolder}/detect/test/detect_demo",// 将要进行调试的程序的路径
            "args": ["--rgb_video","/data/data/videos/car1_20170525.mp4"],// 程序调试时传递给程序的命令行参数
            "stopAtEntry": false,// 设为true时程序将暂停在程序入口处,一般设置为false
            "cwd": "${workspaceRoot}/detect/test",// 可执行程序的启动路径
            "environment": [],
            "externalConsole": true,// 调试时是否显示控制台窗口,一般设置为true显示控制台
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

3.若出现问题:Unable to start debugging. Failed to initialize debugger terminal.在launch.json中添加如下配置

"pipeTransport": {
    "pipeCwd": "/usr/bin",
    "pipeProgram": "/bin/sh",
    "debuggerPath": "/usr/bin/gdb",
    "pipeArgs": [
        "-c"
    ]
},
  • 调试中加编译
    1.生成task.json
    输入ctrl+p命令,输入>tasks:configure,如下图所示
    这里写图片描述
    这里写图片描述
    这里写图片描述
    生成的tasks.json如下图所示
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo Hello"
        }
    ]
}

2.在launch.json的“cwd”后添加"preLaunchTask": "Make detect demo",其中Make detect demo需要与tasks.json中的label一致。

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Make detect demo",//与launch.json的文件中的preLaunchTask保持一致
            "type": "shell",
            "command": "make -j8",
            //"command": "./build.sh",//此处也可将所要执行的命令写在shell脚本里支执行
            "options": {
                "cwd": "build"//进入到build文件夹
            },
            "args": [
            ], // 程序调试时传递给程序的命令行参数
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

3.按F5键开始调试,会自动编译最新的文件

猜你喜欢

转载自blog.csdn.net/u013187057/article/details/82687429