Configure c + + for vscode under linux to debug it like visual studio 2019

Keywords: C++ Linux Visual Studio Code

c + + compiling mode under linux

        Under win, visual studio has carried out a large number of encapsulation for us, and designed a large number of easy-to-use configurations and interfaces, so that developers pay too much attention to the underlying compilation and linking process, and only need to pay attention to the code itself, which improves the development efficiency. However, it is different under linux. We need to do the compilation and linking under linux, Therefore, you will find that there are many ways to compile links under linux. In this way, only three sets of Makefile, cmake and vscode are discussed:

Makefile

        The source here is not introduced, but directly to the tutorial. You can see it This article

cmake

        cmake is a further encapsulation of Makefile, so that developers don't have to spend a lot of time writing makefile. They only need a few simple commands to complete the compilation and link operation. For a detailed tutorial, you can see my previous articles or other people's articles. It's not difficult to understand

VScode c + + environment configuration

        Although both Makefile and cmake can compile and link c + +, because the ide of vs2019 used for win development prefers to compile, link and debug on the IDE, the best IDE is vscode. Vscode is not a compiler for C + +. It can be said that it is a tool and customizable programming ide. I need to configure it to achieve my goal, How to make it like vs2019? Next, we will describe in detail how to configure:

1. Project description

        The experimental project needs to rely on opencv libraries, and there are also multiple source cpp files. If you call it through main, you need to configure vscode to support the compilation and linking of multiple files, as well as the dependence on external libraries. Let's start here. The directory structure of the files is as follows:

  The above cmakelists.txt and build folders are ignored. They are operated by cmake. The directory only contains src and main.cpp of the root directory. The source file of src depends on opencv. At the same time, src has a large number of CPP files.

2. Configure vscode environment

        In the first step, you should know that the three files required for vscode to configure the c + + environment are tasks.json, launch.json and c_cpp_properties.json, these three files. You can see about these three files This article , most other articles on the Internet are hello word. This configuration is very simple, but the complex configuration is the file compilation and debugging configuration under multiple files, multiple files and multiple directories. It will be introduced in detail here. First of all, you need to understand that whether cmake, vscode or others are compiled and linked through Makefile, The difference is that they further encapsulate the Makefile, so what we configure vscode is actually just the command to configure the Makefile. It's easy to understand this step,

Look at C first_ cpp_ Properties.json file:

  The biggest function of this file is to include header files, so that you can find functions when writing c + +, and the code will highlight without wavy prompts. Therefore, you should add header files of your external libraries, such as opencv header files

launch.json file

  This file is mainly used to add gdb debugging tasks

tasks.json file

  This file is very important. It is the main configuration file for compiling links. Usually, if there are multiple folders, you can add the source file directory directly -g later, which can be. cpp and. c files, and then add them automatically by using wildcards. If you rely on a third-party library, you can add them directly later. Among them:

-I (capital i) means to include a directory

-L means include library directory

-L (lowercase L) means link library

Many other commands can be added here, so you need to study the Makefile commands, because the above commands are Makefile commands.

Commissioning screen:

  Trace to source code

For your convenience:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ Generate active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-std=c++11",
                "-g",
                "${file}",
                "${fileDirname}/src/*.cpp",
                //"${fileDirname}/*.c",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                // Set up dynamic link library
                "-I", "/usr/local/include",
                "-I", "/usr/local/include/opencv2",
                "-I", "/usr/local/include/opencv",
                "-L", "/usr/local/lib",
                "-l", "opencv_videoio",
                "-l", "opencv_ml",
                "-l", "opencv_videostab",
                "-l", "opencv_features2d",
                "-l", "opencv_calib3d",
                "-l", "opencv_video",
                "-l", "opencv_superres",
                "-l", "opencv_objdetect",
                "-l", "opencv_flann",
                "-l", "opencv_dnn",
                "-l", "opencv_stitching",
                "-l", "opencv_imgproc",
                "-l", "opencv_highgui",
                "-l", "opencv_photo",
                "-l", "opencv_core",
                "-l", "opencv_imgcodecs",
                "-l", "opencv_shape",
            ],
            "options": {
                //"cwd": "${fileDirname}"
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Debugger generated tasks."
        }
    ],
    "version": "2.0.0"
}

Posted by ultk on Mon, 06 Dec 2021 16:48:56 -0800