The following content is aimed at the Linux operating system (including the Windows Subsystem for Linux WSL2).
This article is a summary of a series of articles about using VSCode to compile and debug C++ code in the Linux system. The first three articles are as follows:
- Explain in detail the whole process of preprocessing, compiling, assembling and linking of C/C++ code
- Use VSCode to debug simple C++ code in Linux environment
- Use VSCode to debug CMake project in Linux environment
1. Fundamental logic
The essential logic of compiling and debugging C++ code in VSCode:
tasks.json
Specifies how to build binary executables- Can be generated directly by the g++ compiler
- Can be generated by CMake
.sh
Can be generated by script
launch.json
Responsible for configuring the gdb debugger , including: specifying the executable file name, command line parameters, and prelaunchTask
2. Solution 1: Directly invoke the g++ compiler to generate executable files
The specific process is explained in detail in this article: Linux environment uses VSCode to debug simple C++ code
Here is a brief overview of key points.
- Step 1 : Pass
*.cpp
the source code file throughg++
the compiler to generate a debuggable executable binary file
If you do not run it in VSCode, but run it in the terminal, you need to run the following command:
g++ -g hello.cpp -o hello
Then configure this step in VSCode tasks.json
, tasks.json
the content of which should be as follows (for the specific process, please refer to the link above):
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: /usr/bin/g++"
}
]
}
- Step 2 : Call the gdb debugger to debug the executable file
If you do not run it in VSCode, but run it in the terminal, you need to run the following command:
gdb hello
Configure this step in VSCode launch.json
, launch.json
the content should be as follows (detailed process and explanation can also refer to the link above):
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"miDebuggerPath": "/usr/bin/gdb",
"preLaunchTask": "C/C++: g++ 生成活动文件"
}
]
}
3. Solution 2: CMake generates executable files
The specific process is explained in detail in this article: Use VScode to debug CMake projects in Linux environment
For CMake projects, if you do not use VSCode, but use the terminal command line to compile, the standard practice is:
cd build
cmake ..
make
Then configure this process in VSCode tasks.json
, and its content should be as follows (see the reference link for the detailed process):
{
"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"]
},
],
}
After completing the CMake compilation process through VSCode, an executable file will be generated in the build directory.
To call gdb to debug the generated executable file, the configuration launch.json
file is required as follows:
{
"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"
}
]
}
4. Solution 3: shell script generates executable file
The content of the script file build_executable.sh
is as follows:
echo "Configuring and building ORB_SLAM3..."
mkdir build
cd build
# cmake .. -DCMAKE_BUILD_TYPE=Debug
cmake .. -Wno-dev
make -j8
This scheme is similar to scheme two, that is, write the process of cd build + cmake + make into the shell script file, then you only need to replace the cmake + make process in scheme two with the execution script file, and directly list the reference and.sh
It can be found that the content of the file has basically remained unchanged:tasks.json
launch.json
launch.json
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "sh",
"args": [
"build_executable.sh"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
}
]
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ - debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}