plug-in required
engineering structure
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${command:cmake.launchTargetPath}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
}
]
}
program": "${command:cmake.launchTargetPath} : Execute the commands in the CMake Tools plugin. Tell VSCode the path to the target executable.
main.cpp
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("hello world\n");
return 0;
}
CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 3.10)
PROJECT(demo)
aux_source_directory(./src DIR_SRC)
add_executable(demo ${
DIR_SRC})
Compile and debug
Press F5 to trigger cmake compilation and construction. The intermediate files generated by the build will be placed in the build directory, which contains several important files.
- compile_commands.json: Compile the database, the clangd plugin will analyze this file and generate a code index. In this way, when we write code later, there will be code hints, and the code will be automatically completed
- demo: The executable file generated after make
To let clangd generate index files, we need to restart VSCode. Every time you start VSCode, clangd will build an index. If the code volume is huge, this process will take a certain amount of time, and the index file will be placed in the .cache directory.
You can use the du command to view the size of the .cache directory:
lorien@ubuntu20:/work2/cmake/cpp$ du -h .cache/
8.0K .cache/clangd/index
12K .cache/clangd
16K .cache/