I just want to have autocompletion for glib-2.0 in my IDE!

Hi. Without much success I’m trying to setup an IDE to work with a CMake project that uses glib-2.0. For instance:

    #include <gio/gnetworking.h>    
    void foo() {
        g_networking_init();
    }

Previously I was using Autotools and Eclipse-CDT. In it there’s a neat feature: “CDT Libtool GCC Build Output Parser”. It liberates you from the need to manually configure preprocessor definitions and include paths added by Autoconf. It parses the output of make, searches for the lines that look like a compiler command and extracts the -D/-I flags. https://i.stack.imgur.com/7uQAv.png

This page is missing in Eclipse projects created from the template “Empty or Existing CMake Project”. Even worse, I can’t find where the include paths could be configured manually. I even tried to add the “CDT Make Nature” to the project but the page added by that nature is ignored: I still get “Unresolved inclusion”. https://i.stack.imgur.com/SwfQ6.png

Then I thought that VSCode for Linux could have more mature support for CMake. Unfortunately, it cannot extract the include paths from CMake output either. Again, I tried to configure them manually: I used IntelliSense GUI to add /usr/include/glib-2.0 and friends. It created c_cpp_properties.json with includePath containing them. Now the #include source lines are not marked as errors, but “Go to Definition” still fails and autocomplete does not work.

What host system are you using / which generator / compiler?

With make or ninja it is possible to generate a compile_commands.json, when generating with cmake pass -DCMAKE_EXPORT_COMPILE_COMMANDS=ON. Or to make your life easier, use CMakePresets.
This will generate a compile_commands.json file in your build folder containing the compiler command for each source file.

A lot of tools / plugins understand this file.

For instance with VS Code:

  • You might wanna use the extension “vs-vscode.cmake-tools”, which is able to copy the compile_commands.json to your root folder if you set the settings "cmake.copyCompileCommands": "${workspaceFolder}/compile_commands.json". This extension offers a lot more and I personally not super convienced of using it as I prefer the command line. But the automatic copy of the compile commands to the root folder (where most tools expect it by default) is quite handy
  • Install the extension “llvm-vs-code-extensions.vscode-clangd” instead of the C++ Microsoft extension. It will pick up the compile_commands.json in your root folder by default and everything should work without any issues. Maybe the Microsoft C++ extension works as well, but personally I really like clangd. If the compile_commmands.json is still in your build folder, you can also change the settings of the clangd extension to pick it up there. Although you need to lookup what exactly the command line argument is, I forgot.

This helped. Thanks. I disabled MS c++, installed vscode-clangd, enabled “copyCompileCommands”. Another question: when I go to declaration of g_networking_init in gnetworking.h the other GLib includes are marked as errors: #include <glib.h>

{
	"resource": "/usr/include/glib-2.0/gio/gnetworking.h",
	"owner": "_generated_diagnostic_collection_name_#0",
	"code": "pp_file_not_found",
	"severity": 8,
	"message": "'glib.h' file not found",
	"source": "clang",
	"startLineNumber": 22,
	"startColumn": 10,
	"endLineNumber": 22,
	"endColumn": 18
}

This is much less annoying, but can this be resolved?

The reason for this might be that the opened file (gnetworking.h) is outside of the context of the current project and therefore clangd doesn’t find anymore the compile_commands.json?
So basically clangd would need to know that it should interpret the header file in the same context as the file you came from.

Maybe you should open an issue here: Issues · clangd/vscode-clangd · GitHub