现在已经进入GTK3的时代很久了,最近了解了一下Ubuntu的相关开发技术,在其开发社区发现了一款不错的开发工具,quickly,感兴趣的朋友可以参考官方教程进行尝试:
http://developer.ubuntu.com/get-started/
该quickly开发套件使用了pygtk来做界面的整合,无意中就去了解了一下gtk3的相关文档,在编译其最基本的例子的时候却出现了错误:
教程网址:http://developer.gnome.org/gtk3/stable/gtk-getting-started.html
开发一个最简单的程序,名为: window-default.c,代码如下:
#include <gtk/gtk.h>
int
main (int argc,
char *argv[])
{
GtkWidget *window;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show (window);
gtk_main ();
return 0;
}
显示效果应该是:
可是进行编译的时候却出问题了:
$gcc `pkg-config --cflags --libs gtk+-3.0` window-default.c/tmp/ccvouAR6.o: In function `main':
window-default.c:(.text+0x17): undefined reference to `gtk_init'
window-default.c:(.text+0x23): undefined reference to `gtk_window_new'
window-default.c:(.text+0x47): undefined reference to `gtk_main_quit'
window-default.c:(.text+0x5b): undefined reference to `g_signal_connect_data'
window-default.c:(.text+0x67): undefined reference to `gtk_widget_show'
window-default.c:(.text+0x6c): undefined reference to `gtk_main'
collect2: ld returned 1 exit status
然后尝试了内容检测和相应库的检查:
$echo `pkg-config --cflags gtk+-3.0` `pkg-config --libs gtk+-3.0`
-pthread -I/usr/include/gtk-3.0 -I/usr/include/atk-1.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include -I/usr/include/freetype2 -I/usr/include/pixman-1 -I/usr/include/libpng12 -lgtk-3 -lgdk-3 -latk-1.0 -lgio-2.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo-gobject -lpango-1.0 -lcairo -lgobject-2.0 -lglib-2.0
#以上内容说明库的路径是OK的,下面再次编译也报错
$gcc `pkg-config --cflags gtk+-3.0` `pkg-config --libs gtk+-3.0` window-default.c
/tmp/cc2wRBJp.o: In function `main':
window-default.c:(.text+0x17): undefined reference to `gtk_init'
window-default.c:(.text+0x23): undefined reference to `gtk_window_new'
window-default.c:(.text+0x47): undefined reference to `gtk_main_quit'
window-default.c:(.text+0x5b): undefined reference to `g_signal_connect_data'
window-default.c:(.text+0x67): undefined reference to `gtk_widget_show'
window-default.c:(.text+0x6c): undefined reference to `gtk_main'
collect2: ld returned 1 exit status
使用命令刷新了动态链接库的路径也不行:
sudo ldconfig
最后按照官方文档,调换了编译命令的顺序,居然一点问题也没有的通过了!!!
gcc `pkg-config --cflags gtk+-3.0` -o window-default window-default.c `pkg-config --libs gtk+-3.0`
只是将待编译的文件放在中间就能够编译通过了!!! 真是奇迹!
$gcc `pkg-config --cflags gtk+-3.0` -o window-default window-default.c `pkg-config --libs gtk+-3.0`
qunengrong@qunengrong-Studio-1450 /media/GoldenResources/PracticePark/gtk+/gtk3/basics
$ls
window-default window-default.c