linux vscode c + + debugging

Basic introduction

When debugging c + + programs with vscode under linux, launch.json and tasks.json will be encountered in the. Vscode file in the workspace. Their functions are as follows:

1. lauch.json is responsible for starting tasks and executing files (executable files)

2. Tasks.json is responsible for configuring related tasks. In short, it is responsible for compiling links and generating executable files. In fact, it is to execute the compilation instructions of the terminal [g++ -g main.cpp -o main.o]. Therefore, tasks.json must be executed before the launch.json file is executed

3. Launch.json and tasks.json are associated through the preLaunchTask in launch.json. The preLaunchTask in launch.json is to start tasks.json, so the label corresponding to preLaunchTask should be consistent with task.json.

  • Parameter introduction  

launch.json

{
    "version": "2,0.0", //The version of the configuration file used to be 0.2.0. The new version has been deprecated and used to be 2.0.0
    "configurations": [ //Configure domain
        {
            "name": "(gdb) Launch", //Name of the configuration file
            "type": "cppdbg", //The type of debugging, which is cpp
            "request": "launch",//There are two types of requests for configuration files: launch and attach
            "targetArchitecture": "x64", //Hardware kernel architecture, 64bit
            "program": "${workspaceRoot}/${fileBasenameNoExtension}.out",//The path and file name of the executable that will be debugged
            "args": [],//The parameter passed in when calling the main function is generally empty
            "stopAtEntry": false,//When set to true, the program will pause at the program entrance, which is generally set to false
            "cwd": "${workspaceFolder}",//Working directory when debugging
            "environment": [],
            "internalConsoleOptions": "openOnSessionStart",//
            "externalConsole": true,//Whether to display the console window during debugging. It is generally set to true
            "MIMode": "gdb",//Specifies the debugger of the connection. You can omit not writing
            //"miDebuggerPath": "C:\MinGW\bin\gdb.exe"
            "setupCommands": [ 
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build",//The task executed before the start of a debugging session, usually a compiler. Corresponding to the label of tasks.json, it is generally a compiler, c + + is g++, c is gcc, and the multi file compilation using cmake is build
        }
    ]
}

tasks.json 

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "label": "Build", // Task name, corresponding to preLaunchTask of launch.json
    "version": "2.0.0",
    "command": "g++",// cpp uses the g + + command and c uses gcc
    "args": [ 
        "-g",
        "${file}", 
        "-o", 
        "${fileDirname}/${fileBasenameNoExtension}.out", 
        "-std=c++11",    //Using c++11
        "-lpthread"      //Link thread library
    ],
    //The absolute path where ${fileDirname} the currently open file is located, excluding the file name
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation":  ["relative", "${workspaceRoot}"],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}

${workspaceRoot} absolute path of the currently open folder + folder name

¥ {workspaceFolder} the path of the current program,. vscode path

${file} currently opens the file name being edited, including absolute path, file name and file suffix

${fileBasename}   Currently open file name + suffix, excluding path

${fileBasenameNoExtension} the file name of the currently open file, excluding the path and suffix

The absolute path where ${fileDirname} the currently open file is located, excluding the file name

${fileExtname} the suffix of the currently open file

Single file debugging

  • step

1. Create a demo.cpp and load its folder with vscode

2. You can directly press F5 to debug

 

Or click the "run and debug" button on the left side of the vscode interface  , Select "create launch.json",

  Select the CPP folder. Here I select the code folder where my demo.cpp is located. The following is the same as pressing F5

3. Select g + + for c + + and gcc for c  

4. After the system is automatically configured, it can be debugged  

  • launch.json and tasks.json configuration

{
    // Use IntelliSense to understand related properties. 
    // Hover to view the description of an existing property.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - Generate and debug activity files",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "by gdb Enable neat printing",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ Generate active file",//The name can be changed to other names. For example, the names under label in build and tasks.json should be consistent
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ Generate active file",//The name can be changed to other names. For example, the name under preLaunchTask in build, lanuch.json should be consistent
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Debugger generated tasks."
        }
    ],
    "version": "2.0.0"
}

Multi file debugging

For multi file debugging, cmake needs to be used, and CMakeLists.txt is used for debugging.

  • preparation

1. CMake and CMake Tools need to be installed under vscode.

2. Use the demo of the last CMakeLists chapter to demonstrate VSCode debugging

The add library is now placed in the subfolder to regenerate the dynamic library, and there is no libadd in the 3rdparty.

  • launch.json and tasks.json configuration

launch.json

{
    // Use IntelliSense to understand related properties. 
    // Hover to view the description of an existing property.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - Generate and debug activity files",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/demo",//Program is the path + name of the current program to be executed, and its name is determined by CMakeLists.txt
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "by gdb Enable neat printing",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

tasks.json 

{   
    "version": "2.0.0",
    "options": {
        "cwd": "${workspaceFolder}/build"
    },
    "tasks": [
        {
            "type": "shell",
            "label": "cmake",
            "command": "cmake",
            "args": [
                ".."
            ]
        },
        {
            "label": "make",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "command": "make",
            "args": [

            ]
        },
        {
            "label": "build",
			"dependsOrder": "sequence", // Execute task dependencies in the order listed
            "dependsOn":[
                "cmake",
                "make"
            ]
        }
    ]

}

Execution rendering

 

  • matters needing attention

To debug, you need to set the debug mode in CMakeLists.txt, otherwise vscode may not enter the breakpoint.

There are two ways to set debug:

  • Cmake is required for debug mode_ CXX_ Flags plus - g

        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g ")

  • Use SET(CMAKE_BUILD_TYPE Debug)

        SET(CMAKE_BUILD_TYPE Debug)

Tags: C++ Linux Visual Studio Code

Posted on Sun, 19 Sep 2021 17:37:05 -0400 by metin