未定义的引用:在 Windows 上编译简单的 GTK+ 3.0 c 程序时链接库

Undefined references: linking libraries when compiling a simple GTK+ 3.0 c-program on windows

提问人:Red Star 提问时间:1/5/2016 最后编辑:Red Star 更新时间:1/12/2016 访问量:2046

问:

我从这里下载了gtk+-bundle_3.4.2-20130513_win64.zip

我已经在“C:/gtk3/”上提取了它,我已将目录添加到%PATH%变量中;

我复制了这个hello.c代码:

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

(反引号在我的 cmd 上不起作用,所以我将命令复制到文本文件并使其成为一个简单的 Makefile)

pkg-config --cflags gtk+-3.0

输出:

-mms-bitfields -IC:/gtk3/include/gtk-3.0 -IC:/gtk3/include/cairo -IC:/gtk3/include/pango-1.0 -IC:/gtk3/include/atk-1.0 -IC:/gtk3/include/cairo -IC:/gtk3/include/pixman-1 -IC:/gtk3/include -IC:/gtk3/include/freetype2 -IC:/gtk3/include -IC:/gtk3/include/libpng15 -IC:/gtk3/include/gdk-pixbuf-2.0 -IC:/gtk3/include/libpng15 -IC:/gtk3/include/glib-2.0 -IC:/gtk3/lib/glib-2.0/include

pkg-config --libs gtk+-3.0

输出

-LC:/gtk3/lib -lgtk-3 -lgdk-3 -lgdi32 -limm32 -lshell32 -lole32 -Wl,-luuid -lpangocairo-1.0 -lpangoft2-1.0 -lfreetype -lfontconfig -lpangowin32-1.0 -lgdi32 -lpango-1.0 -lm -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl

然后我把整个命令放在一个 Makefile 中,就像这样

gcc hello.c $(pkg-config --cflags gtk+-3.0) $(pkg-config --libs gtk+-3.0)

我还尝试了所有可能的订单,例如:

gcc $(pkg-config --cflags gtk+-3.0) hello.c $(pkg-config --libs gtk+-3.0)

gcc $(pkg-config --cflags gtk+-3.0) $(pkg-config --libs gtk+-3.0) hello.c

gcc hello.c $(pkg-config --libs gtk+-3.0) $(pkg-config --cflags gtk+-3.0)

gcc $(pkg-config --libs gtk+-3.0) hello.c $(pkg-config --cflags gtk+-3.0)

在上述所有情况下,当我使用 gcc 编译时,我得到了这个

C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0xd): undefined reference to `gtk_application_window_new'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0x15): undefined reference to `gtk_window_get_type'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0x28): undefined reference to `g_type_check_instance_cast'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0x38): undefined reference to `gtk_window_set_title'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0x3d): undefined reference to `gtk_window_get_type'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0x50): undefined reference to `g_type_check_instance_cast'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0x68): undefined reference to `gtk_window_set_default_size'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0x73): undefined reference to `gtk_widget_show_all'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0x97): undefined reference to `gtk_application_new'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0xcf): undefined reference to `g_signal_connect_data'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0xd4): undefined reference to `g_application_get_type'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0xe8): undefined reference to `g_type_check_instance_cast'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0xfe): undefined reference to `g_application_run'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0x10e): undefined reference to `g_object_unref'
collect2.exe: error: ld returned 1 exit status

我正在使用 Windows 7 64 位和 cmd

我做错了什么?

C 编译 gtk windows-7-x64 undefined-reference

评论

1赞 Lynn Crumbling 1/5/2016
您正在使用而不是在链接参数中:..尝试。同样的事情包括:/\ -LC:/gtk3/lib-LC:\gtk3\lib-IC:\gtk3\include\gtk-3.0
0赞 Red Star 1/5/2016
我把所有的东西都变成了'\',这样仍然得到了相同的结果/gcc hello.c -mms-bitfields -IC:\gtk3\include\gtk-3.0 -IC:\gtk3\include\cairo -IC:\gtk3\include\pango-1.0 -IC:\gtk3\include\atk-1.0 -IC:\gtk3\include\cairo -IC:\gtk3\include\pixman-1 -IC:\gtk3\include -IC:\gtk3\include\freetype2 -IC:\gtk3\include [...]
0赞 andlabs 1/5/2016
'/' 无关紧要。真正的问题是调用是错误的。pkg-config
0赞 Red Star 1/5/2016
太老了?如果您认为问题出在捆绑包本身,您能否给我一个可以在 Windows 7 64 位上运行的多合一捆绑包?
0赞 andlabs 1/5/2016
@RedStar您确定您在命令行中尝试过,而不是?--flags--cflags

答:

-1赞 Red Star 1/12/2016 #1

该问题可能是由编译器或 GTK3 软件包引起的 解决方案:卸载所有内容,下载 MSYS2 并下载所需的软件包 (您可以在他们的网站上找到如何通过 MSYS 搜索和安装软件包) (网站在GTK3的官方下载页面提供)

然后一切正常, 无论如何,谢谢