当 C 使用起始代码编译 GTK 库时,对“gtk_application_window_new”的未定义引用

undefined reference to `gtk_application_window_new' when C compiling GTK library with starter code

我对 C 完全陌生,我有兴趣在我的 C 项目中使用 GTK 创建一个 GUI。

我使用 windows 11,我按照 GTK 网站上的所有说明进行 windows 安装,现在我的大部分问题都已解决,但还有最后一个问题是:

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0x15): undefined reference to `gtk_application_window_new'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0x1e): undefined reference to `gtk_window_get_type'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0x2d): undefined reference to `g_type_check_instance_cast'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0x3f): undefined reference to `gtk_window_set_title'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0x44): undefined reference to `gtk_window_get_type'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0x53): undefined reference to `g_type_check_instance_cast'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0x66): undefined reference to `gtk_window_set_default_size'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0x72): undefined reference to `gtk_widget_show_all'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0xa1): undefined reference to `gtk_application_new'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0xd6): undefined reference to `g_signal_connect_data'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0xdb): undefined reference to `g_application_get_type'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0xea): undefined reference to `g_type_check_instance_cast'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0xfc): undefined reference to `g_application_run'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0x10b): undefined reference to `g_object_unref'
collect2.exe: error: ld returned 1 exit status

我使用以下命令编译我的 C 文件: gcc Interface.c -IC:/msys64/mingw64/bin/../include/gtk-3.0 -IC:/msys64/mingw64/bin/../include/pango-1.0 -IC:/msys64/mingw64/bin/../include -IC:/msys64/mingw64/bin/../include/glib-2.0 -IC:/msys64/mingw64/bin/../lib/glib-2.0/include -IC:/msys64/mingw64/bin/../include/harfbuzz -IC:/msys64/mingw64/bin/../include/freetype2 -IC:/msys64/mingw64/bin/../include/libpng16 -mms-bitfields -IC:/msys64/mingw64/bin/../include/fribidi -IC:/msys64/mingw64/bin/../include/cairo -IC:/msys64/mingw64/bin/../include/lzo -IC:/msys64/mingw64/bin/../include/pixman-1 -mms-bitfields -mms-bitfields -mms-bitfields -mms-bitfields -mms-bitfields -mms-bitfields -mms-bitfields -IC:/msys64/mingw64/bin/../include/gdk-pixbuf-2.0 -mms-bitfields -mms-bitfields -mms-bitfields -IC:/msys64/mingw64/bin/../include/atk-1.0 -mms-bitfields -mms-bitfields -mms-bitfields -pthread -mms-bitfields

我用Visual Studio代码。

起始代码:

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

你能建议我解决问题的方法吗?我不知道为什么会这样,因为我对 C 的了解很少。

我遵循了所有其他类似问题的答案,但 none 对我很有用。 谢谢!

这是一个 link 错误,不是编译错误,所以我猜你至少在你的 link 库依赖项中缺少对库 gtk-3 的引用(负责gtk_application_window_new 参考)。可能还有其他人失踪了。

您可以在此处查看:https://docs.gtk.org/gtk3/compiling.html 以获取您可能需要的示例。