Use VScode to debug CMake project in Linux environment

In the previous article, I introduced the process of compiling simple C++ code using g++ in Linux. This article will introduce how to compile and debug a CMake project in VSCode.

If you don't understand the C++ compilation process, g++ compiler, and gdb debugger, you can read these two articles first:

Why use CMake? Dr. Gao Xiang's "Visual SLAM Fourteen Lectures" gives a good explanation:

In theory, any C++ program can be compiled with g++. But when the scale of the program is getting bigger and bigger, a project may have many folders and source files, and the compilation command input at this time will become longer and longer. Typically, a small C++ project may contain more than a dozen classes, and there are complex dependencies among the classes. Part of it needs to be compiled into an executable file, and the other part into a library file. If you only rely on the g++ command, you need to input a large number of compilation instructions, and the entire compilation process will become extremely cumbersome. Therefore, for C++ projects, it will be more efficient to use some project management tools.
CMake is a convenient C++ project management tool.

1. Demo file directory

The CMake project directory used for demonstration in this article is as follows:

|—— build
|—— helloCMake.cpp
|—— CMakeLists.txt

buildThe folder is used to store the intermediate files generated by cmake. The contents of the other two files are as follows:

// helloCMake.cpp
#include <iostream>
using namespace std;
int main(int argc, char **argv){
    
    
	cout << "Hello, VSCode and CMake!" << endl;
    
    if ( argc >=2 ) {
    
    
        cout << "args[1]: " << string(argv[1]) << endl;
        cout << "args[2]: " << string(argv[2]) << endl;
    }

    return 0;
}
# CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(vscode_cmake)

add_executable(helloCMake helloCMake.cpp)

2. CMake compile

If you do not use VSCode, but use the terminal command line to compile cmake, the standard practice is:

cd build
cmake ..
make

In this way, buildthe cmake intermediate file and a final executable file will be generated in the directory helloCMake, and the project directory will be as follows:

├── build
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   ├── Makefile
│   ├── cmake_install.cmake
│   └── helloCMake
├── CMakeLists.txt
└── helloCMake.cpp

Execute helloCMake(with parameters para1and para2), the result is:

./helloCMake para1 para2
Hello, VSCode and CMake!
args[1]: para1
args[2]: para2

3. Implement CMake compilation in VSCode

So, how to configure the above process in VSCode ? Specific steps are as follows:

  1. In the main menu of VSCode, select Terminal>Configure Default Build Task ,
  2. Select "CMake: build"
  3. A tasks.jsonfile , just replace the contents with the following:
{
    
    
	"version": "2.0.0",
	"tasks": [
        {
    
    
            "label": "cmake",
            "type": "shell",
            "command": "cmake",
            "args": [
                "../"
            ],
            "options": {
    
    
                "cwd": "${fileDirname}/build"
            },            
        },
        {
    
    
            "label": "make",
            "type": "shell",
            "command": "make",
            "args": [],
            "options": {
    
    
                "cwd": "${fileDirname}/build"
            }, 
        },
        {
    
    
            "label": "build",
            "dependsOn":["cmake", "make"]
        },
    ],
}

It can be seen that the above tasks.jsonfile mainly contains three commands:

  • The task with label cmake: Execute the cmake command of shell type, its parameter is ../, and the directory where it is executed is ${fileDirname}/build. This command is equivalent to buildexecuting in the directorycmake ../
  • The task with label make: Execute the make command of shell type, without parameters, and the directory where it is executed is ${fileDirname}/build. This command is equivalent to buildexecuting in the directorymake
  • buildTask with label : This task is composed of cmake and make tasks, that is, the process of executing the above two commands is combined into a build task.

Therefore, executing buildthe task is equivalent to executing cmake ../the and maketwo commands in the build directory to complete the CMake compilation process.

  • In the main menu of VSCode, select Terminal>Run Task... , then select build , and then select "continue without scanning the task output" , you can see in the terminal display interface below the editor that VSCode has completed both cmake and make A task:
    cmake-taskmake task
    the project file at this time will be the same as the CMake compilation using the command line in Section 2, because the essence of the two methods is exactly the same.

4. Debug CMake project code in VSCode

As mentioned above, after completing the CMake compilation process, an executable file will be generated in the build directory helloCMake. The following will introduce how to debug it in VSCode:

  1. In the upper menu of VSCode, select Run -> Add Configuration, and a blank launch.jsonfile will be generated:
 {
    
    
    "version": "0.2.0",
    "configurations": []
}
  1. All we have to do is to tell VSCode in this file: use gdb to debug the executable file generated earlier, and launch.jsonadd the following content to the file:
{
    
    
    "version": "0.2.0",
    "configurations": [
        {
    
    
            "name": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/build/${fileBasenameNoExtension}",
            "args": ["para1", "para2"],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
    
    
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

in,

  • "program": It is used to specify the executable file to be debugged, here it is referred to by a variable name, and its value ishelloCMake
  • "args": When executing the code, the command line parameters that need to be added
  • prelaunchTask: Before executing gdb debugging, the tasks that need to be executed in advance, here is set to "build", is to specify the build task configured in Section 3, that is, before gdb debugging, first execute cmake and make
  1. Go back to the helloCMake.cpp file, set a breakpoint, and press F5 to realize code debugging:
    Debug

5. Reference Tutorial

  1. Use VS Code + CMake to debug c++ programs under Linux
  2. Use VSCode to debug simple C++ code in Linux environment

Guess you like

Origin blog.csdn.net/hypc9709/article/details/129430861