It is slightly tricky to start a C/CPP project in vscode. Of course we can use it as just a text editor and compile manually with command line, but then you need to bare with all of the “variable not found” errors.
We need Three ingredients
- A build system: CMake is a modern choice
- A language server: To be able to jump between deceleration, definition, auto completion, etc.
- Debugging infrastructure
The choice for build system is clear: CMake. However, for language server and debugging, there are a number of solutions. First solution is to use Microsoft’s Intellisense C/CPP support, that is both a debugging tool, and a language server. The second choice is use clangd as the language server, and use Intellisense only for the debugging.
clangd and CMake
The setup is not very hard. We need to specify the project structure in CMake. We need to declare the executable targets, include paths, libraries, etc. In order to make the build process project dependent, we need to specify project-dependent configurations in CMakePresets.json.
The crucial part to make clangd work is to enable CMake to generate compile commands. These are all the commands that CMake will run according to our configuration, and this file will let clangd to determine where are the source files, header files, libraries, etc.
However, when CMake generates compile_commands.json, it is inside the build directory (which we specify in CMakePresets.json), and clangd can not find it automatically. There are two solutions around this
- generate a config file
.clangdand specify
CompileFlags:
CompilationDatabase: path/to/folder/containing/commands- The second way, is to ask
CMaketo make a copy ofcompile_commands.jsonin the root of the directory, whereclangdcan easily discover it. For this we need to add to theCMakePresests.jsonthe following setting
"cmake.copyCompileCommands": "${workspaceFolder}/compile_commands.json"To make it permanent, you can add this setting to the user setting json file > Preferences: Open User Settings (JSON)
Also, it is a good idea to add the following permanent settings to the file above
"cmake.configureOnOpen": true,
"cmake.exportCompileCommandsFile": true,
"cmake.modifyLists.addNewSourceFiles": "ask",
"cmake.copyCompileCommands": "${workspaceFolder}/compile_commands.json"A minimal setup for the CMakeLists.txtis
cmake_minimum_required(VERSION 3.10.0)
project(quad VERSION 0.1.0 LANGUAGES C CXX)
add_executable(quad main.cpp
src/adder.cpp
)
target_include_directories(quad PUBLIC
./inc
)Summary
After opening a folder in vscode, make sure CMake Tools and clangd are installed. Make the project structure (i.e. src, inc, etc directories). Then by > CMake: Quick Start start a simple CMakeLists.txt and CMakePresets.json. Add the address to the include folders, source folders and etc. Make sure Preferences: Open User Settings (JSON)is as above. After building the project, compile_comands.json should appear in the root. Do > clangd: Restart language server, to make sure that the changes are effective.
Comments & reactions