Yammins
Yammins

Reputation: 81

Unable to compile GTK4 on Fedora with Hello World example Despite having all the requisite packages

I have read the install instructions on GTK for GTK4, which I used

sudo dnf install gtk4 gtk4-devel

The code of the Hello, World example is

#include <gtk/gtk.h>

static void
print_hello (GtkWidget *widget,
             gpointer   data)
{
  g_print ("Hello World\n");
}

static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;
  GtkWidget *button;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);

  button = gtk_button_new_with_label ("Hello World");
  g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
  gtk_window_set_child (GTK_WINDOW (window), button);

  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_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

I used the compile function for GCC:

gcc -o hello-world-gtk hello-world-gtk.c `pkg-config --cflags --libs gtk4`

But this error presents

gtktest.c:1:10: fatal error: gtk/gtk.h: No such file or directory
    1 | #include <gtk/gtk.h>
      |          ^~~~~~~~~~~

As I see, I should have all the requisite packages installed and I am unsure why the compiler cannot pull the gtk library. Any help would be awesome.

Upvotes: 3

Views: 530

Answers (1)

Yammins
Yammins

Reputation: 81

Alright I found a strange solution.

For whatever reason, this did not work using their pasted compile code, however when I switched to the below, it worked just fine.

gcc `pkg-config --cflags gtk4` hello.c -o hello `pkg-config --libs gtk4`

Upvotes: 4

Related Questions