2

我已阅读我使用的 GTK for GTK4 的安装说明

sudo dnf install gtk4 gtk4-devel

Hello, World 示例的代码是

#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;
}

我使用了 GCC 的编译功能:

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

但是这个错误呈现

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

如我所见,我应该安装了所有必需的软件包,但我不确定为什么编译器无法提取 gtk 库。任何帮助都是极好的。

4

1 回答 1

4

好吧,我找到了一个奇怪的解决方案。

无论出于何种原因,这在使用他们粘贴的编译代码时不起作用,但是当我切换到下面时,它工作得很好。

gcc `pkg-config --cflags gtk4` hello.c -o hello `pkg-config --libs gtk4`
于 2021-09-10T17:57:04.473 回答