includegtkc-preprocessorarchlunarvim

Clangd with Gtk on LunarVim emitting too many errors, compiling and executing although


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: Errors

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


Solution

  • 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.json file which is necessary for Clangd to analyze your code appropriately. From their documentation:

    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 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

    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.c file, put a file named CMakeLists.txt with the following inside (of course, you need to install CMake):

    # Basic, necessary CMake stuff.
    cmake_minimum_required(VERSION 3.20)
    project(SO78044813 LANGUAGES C)
    
    # Will generate the `compile_commands.json` automatically.
    set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
    
    # Make sure the GTK dependency is imported.
    find_package(PkgConfig)
    pkg_check_modules(GTKMM REQUIRED gtk+-3.0)
    
    # Tell the name of your target executable and the source files
    # which are required to build it.
    add_executable(example.out main.c) 
    

    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:

    set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
    

    This line tells CMake to generate the needed compile_commands.json file.

    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:

    main.c
    CMakeLists.txt
     |
     +- build/
    

    And you should be inside the build directory. From there, run the following command:

    cmake ..
    

    You should see output looking like this:

    -- The C compiler identification is GNU 11.4.0
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working C compiler: /usr/bin/cc - skipped
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.2") 
    -- Checking for module 'gtk+-3.0'
    --   Found gtk+-3.0, version 3.24.33
    -- Configuring done (0.3s)
    -- Generating done (0.0s)
    -- Build files have been written to: /home/stackOverflow/build
    

    In the build directory, a bunch of stuff will be generated. The compile_commands.json file should be there. Copy it beside your main.c file.

    From there, launch LunarVim and the errors should disappear.

    Notes: