I have two executables that are build from the same source (a client and a server) and they're built with the compile options -D CLIENT=0 -D SERVER=1
for the server and -D CLIENT=1 -D SERVER=0
for the client. If I do something like
if (CLIENT) {
// Client specific code
}
clangd complains that CLIENT is not defined. Is there a way to make clangd aware of those macros? (The code compiles just fine, the errors are from clangd
, not the compiler)
Is there a way to make clangd aware of those macros?
From getting started with clangd:
Project setup
To understand source code in your project, clangd needs to know the build flags. (This is just a fact of life in C++, source files are not self-contained.)
By default, clangd will assume that source code is built as clang some_file.cc, and you’ll probably get spurious errors about missing #included files, etc. There are a couple of ways to fix this.
compile_commands.json
compile_commands.json file provides compile commands for all source files in the project. This file is usually generated by the build system, or tools integrated with the build system. Clangd will look for this file in the parent directories of the files you edit. Other tools can also generate this file. See the compile_commands.json specification.
compile_commands.json
is typically generated with CMake build system, but more build systems try to generate it.
I would suggest moving your project to CMake, in the process you will learn this tool that will definitely help you in further C-ish development.
compile_flags.txt
If all files in a project use the same build flags, you can put those flags, one flag per line, in compile_flags.txt in your source root.
Clangd will assume the compile command is clang $FLAGS some_file.cc.
Creating this file by hand is a reasonable place to start if your project is quite simple.
If not moving to cmake, create a compile_flags.txt
file with the content for example like the following, and clangd
should pick this file up:
-DCLIENT=1
-DSERVER=1