未定义的引用:在 windows 上编译简单的 GTK+ 3.0 c 程序时链接库
Undefined references: linking libraries when compiling a simple GTK+ 3.0 c-program on windows
我从 here
下载了 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
我做错了什么?
问题可能是由编译器或 GTK3 软件包引起的
解决办法:全部卸载,下载MSYS2,下载需要的包
(你可以在他们的网站上找到如何通过 MSYS 搜索和安装包)
(网站在GTK3官方下载页面提供)
然后一切正常,
还是谢谢
我从 here
下载了 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
我做错了什么?
问题可能是由编译器或 GTK3 软件包引起的 解决办法:全部卸载,下载MSYS2,下载需要的包 (你可以在他们的网站上找到如何通过 MSYS 搜索和安装包) (网站在GTK3官方下载页面提供)
然后一切正常, 还是谢谢