ubuntu+vscode+C/C++ and single-step debugging

Vscode installs and configures the c++ environment

There are other article references to be added

c++ plugin

Recommended plug-ins
C/C++ (compiler)
Code Runner (compilation and debugging tools)
C++ Intellisense (automatic completion)
Visual Studio IntelliCode (refactoring, inspection)

Compile and debug (compile&debug)

The general configuration file
launch.json
is responsible for mobilizing executable files, running files or debugging files.

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

tasks.json

{
    
    
    "version": "2.0.0",
    "tasks": [
        {
    
    
            "type": "cppbuild",
            "label": "compile",
            "command": "/usr/bin/g++-5",
            "args": [
                "-g",
                "-std=c++11",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
    
    
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "调试器生成的任务。"
        }
    ]
}

Responsible for organizing files for compilation and generating executable files. Its function can be compared to the Makefile or qt pro file, so the selection of compilation options and compilation tools is performed here.

Parameters of the compilation tool: command

The main pitfall is whether there are multiple g++ compilers in the current system. If there is only one, you only need to configure the command option as "command": "g++". If there are more than one, you need something like "command": "/ usr/bin/g+±5" to write the compiler path and name correctly, the method to check the compiler version is as follows:

sudo update-alternatives --query g++

The following results can be obtained.

freja@freja-virtual-machine:~$ sudo update-alternatives --query g++
Name: g++
Link: /usr/bin/g++
Status: manual
Best: /usr/bin/g++-5
Value: /usr/bin/g++-5

Alternative: /usr/bin/g++-5
Priority: 60

Alternative: /usr/local/bin/g++
Priority: 50

From the above results, we can see that the priority number of /usr/bin/g+±5 is 60, which is greater than the priority number of 50 of /usr/local/bin/g++, so the default compiler of the current system is /usr/bin/g+± 5.
If the compiler specified in the command is different from the default compiler of the system, it will compile and run, but there is no way to stop at the breakpoint (hit the breakpoint, single-step debugging). Therefore, keeping the command compiler consistent with the system compiler is the key to hitting the breakpoint.

Arguments for compile options: args

In addition, debugging also needs to add -g to the compilation option args. -g is the basic option for debugging.
If you need to use the features of c++11, you need to add -std=c++11 to the compilation option to By analogy, you can get the features of c++14 or c++17.

"args": [
    "-g",
    "-std=c++11",
    "${file}",
    "-o",
    "${fileDirname}/${fileBasenameNoExtension}"
],

generate filename

The last parameter of args is the name of the target file. In order to be used with the launch.json file, please ensure that the last parameter of args in tasks.json is consistent with the program parameter in launch.json.
fileBasenameNoExtension is the name of the target file without the suffix.
fileDirname is the combination of the address of the folder where the current file is located
${fileDirname}/${fileBasenameNoExtension}so that the generated target file name is consistent with the name of the file where the main function is located (no suffix)

Single-file compilation and multi-file compilation

Single file: "${file}",
multiple files: "*.cpp",

"args": [
    "-g",
    "-std=c++11",
    "*.cpp",
    "-o",
    "${fileDirname}/${fileBasenameNoExtension}"
],

attached

PS: For multi-version g++ installation and version selection, please refer to my other article.
g++11 compile and install

Guess you like

Origin blog.csdn.net/sinat_21699465/article/details/119186091