I'm new to the development world, and I was trying to use GTK4 to develop an application in C using LunarVim on my ArchLinux (WSL2 By the way) using the ZSH framework for shell.
So everything was going fine while configuring, but when I tested the example first application that the docs site gave https://docs.gtk.org/gtk4/getting_started.html; the preprocessor identified a lot of unknown types, declarations and identifiers, but when I compile it using the command given in the docs it goes just fine and when it is executed, works as fine as the compiling.
In short, even though I get errors on my text editor I can compile it and run it.
Is there a problem? How can I fix those mean errors? Why can I compile and run with those errors?
I installed gtk with: yay -S gtk4
Here is how it looks on my editor:

Here is the code sample I used:
#include <gtk/gtk.h>
static void
activate (GtkApplication* app,
gpointer user_data)
{
GtkWidget *window;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
gtk_window_present (GTK_WINDOW (window));
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
Here is the command I use to compile:
gcc -o hello_world $(pkg-config --cflags --libs gtk4) hello_world.c
All the headers files are in the place they should be in /usr/include/gtk-4.0
I think your problem is not with LunarVim, nor with GTK, but rather with your LSP (i.e. Clangd). The LSP (Language Server Protocol) is the software that looks at your code and shows you error, suggestions, etc. It also allows you to move faster in your code. This is separate from the compiler (here GCC) and the editor (LunarVim). It seems that in your case, it is not configured well.
From the given information (cannot be sure), I think your are missing the
compile_commands.jsonfile which is necessary for Clangd to analyze your code appropriately. From their documentation:I think this is your issue. I have faced the same problem with Gtkmm and Vim8 and the simplest solution I found was to let CMake generate this file for me.
Here is how to do this, if you are interested in trying CMake.
Next to your
main.cfile, put a file namedCMakeLists.txtwith the following inside (of course, you need to install CMake):Notice I am using gtk+-3 here, this is because I don't have Gtk4. You will need to adjust here. Also, note the following line:
This line tells CMake to generate the needed
compile_commands.jsonfile.Once you have this file, create a build directory and move inside it. You should have something like the following on your disk so far:
And you should be inside the
builddirectory. From there, run the following command:You should see output looking like this:
In the
builddirectory, a bunch of stuff will be generated. Thecompile_commands.jsonfile should be there. Copy it beside yourmain.cfile.From there, launch LunarVim and the errors should disappear.
Notes:
compile_commands.jsonis just a JSON file with information about how you build your stuff. Technically, you could write it by hand (the specification is available here) but this approach doesn't scale.compile_flags.txtfile, which is global to the whole project. I have never used one myself, but if your project is relatively small, this could do the job. One downside is that all files in your project inherit the same compilation flags.