When I use clangd to autocomplete code, I need to use compile_flags.txt to specify the header file directories for clangd. Clangd will parse the header files in these directories for code completion.
For some C++ libraries, such as the GTK library, you can use the pkg-config tool to get all of its header file directories and add them.
>pkg-config --cflags gtk+-2.0
-mms-bitfields -ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/include/gtk-2.0 -ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/lib/gtk-2.0/include -ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/include/atk-1.0 -ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/include/cairo -ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/include/gdk-pixbuf-2.0 -ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/include/pango-1.0 -ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/include/glib-2.0 -ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/lib/glib-2.0/include -ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/include -ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/include/freetype2 -ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/include/libpng14
So you need to add these header file directories into compile_flags.txt.
-ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/include/gtk-2.0
-ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/lib/gtk-2.0/include
-ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/include/atk-1.0
-ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/include/cairo
-ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/include/gdk-pixbuf-2.0
-ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/include/pango-1.0
-ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/include/glib-2.0
-ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/lib/glib-2.0/include
-ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/include
-ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/include/freetype2
-ID:/BuildTools/gtk+-bundle_2.22.0-20101016_win32/include/libpng14
On a Windows system using the mingw compiler, if you have the msvc compiler installed, clangd might use the msvc header file directories instead of the mingw ones for parsing. This can cause some problems in projects compiled with mingw
So you also need to specify the Mingw header file directory in compile_flags.txt, to get all of its header file directories, you can use this command
echo > nul.txt && g++ -v -E -x c++ nul.txt 2> flags.txt
Open the output flags.txt file and find this part
#include <...> search starts here:
D:/BuildTools/i686-7.3.0-release-posix-dwarf-rt_v5-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.3.0/include/c++
D:/BuildTools/i686-7.3.0-release-posix-dwarf-rt_v5-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.3.0/include/c++/i686-w64-mingw32
D:/BuildTools/i686-7.3.0-release-posix-dwarf-rt_v5-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.3.0/include/c++/backward
D:/BuildTools/i686-7.3.0-release-posix-dwarf-rt_v5-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.3.0/include
D:/BuildTools/i686-7.3.0-release-posix-dwarf-rt_v5-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.3.0/include-fixed
D:/BuildTools/i686-7.3.0-release-posix-dwarf-rt_v5-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.3.0/../../../../i686-w64-mingw32/include
End of search list.
So I just need to add these contents to compile_flags.txt.
-ID:/BuildTools/i686-7.3.0-release-posix-dwarf-rt_v5-rev0/mingw32/lib/gcc/i686-w64-mingw32/7.3.0/include/c++
-ID:/BuildTools/i686-7.3.0-release-posix-dwarf-rt_v5-rev0/mingw32/lib/gcc/i686-w64-mingw32/7.3.0/include/c++/i686-w64-mingw32
-ID:/BuildTools/i686-7.3.0-release-posix-dwarf-rt_v5-rev0/mingw32/lib/gcc/i686-w64-mingw32/7.3.0/include/c++/backward
-ID:/BuildTools/i686-7.3.0-release-posix-dwarf-rt_v5-rev0/mingw32/lib/gcc/i686-w64-mingw32/7.3.0/include
-ID:/BuildTools/i686-7.3.0-release-posix-dwarf-rt_v5-rev0/mingw32/lib/gcc/i686-w64-mingw32/7.3.0/include-fixed
-ID:/BuildTools/i686-7.3.0-release-posix-dwarf-rt_v5-rev0/mingw32/i686-w64-mingw32/include
Here it converts relative paths like ../; it should also work if not converted.
If clangd shows undefined symbols during parsing, you need to check whether the header file directory paths in compile_flags.txt are correct. Also, make sure the clangd working directory is set to the project root, and compile_flags.txt should be placed in the project root as well.