在这边说明一下如何在Windows作业系统之下撰写、编译GTK,GTK使用GLib、cairo、Pango、ATK、GTK+、gettext- runtime、libpng、libjpeg、libtiff与zlib,所以您必须也取得这些套件,在GTK官方网站的 Windows Download 中可以取得,最简单的方法是取得all-in-one bundle。
以下说明安装的步骤:
这样就完成Windows下撰写GTK所需的安装了,您可以执行以下的指令取得include目录资讯并储存至inc.txt:
$ pkg-config --cflags --libs gtk+-2.0 > inc.txt
执行以下的指令取得lib目录资讯并储存至lib.txt:
$ pkg-config --libs --libs gtk+-2.0 > lib.txt
为了方便起见,可撰写一个简单的Makefile如下,并将方才的inc.txt与lib.txt之内容分别设定给CFLAGS与LIBS:
* Makefile
**CC = gcc
CFLAGS = -mms-bitfields -IC:/Winware/Gtk/include/gtk-2.0
-IC:/Winware/Gtk/lib/gtk-2.0/include -IC:/Winware/Gtk/include/atk-1.0
-IC:/Winware/Gtk/include/cairo -IC:/Winware/Gtk/include/pango-1.0
-IC:/Winware/Gtk/include/glib-2.0 -IC:/Winware/Gtk/lib/glib-2.0/include
-IC:/Winware/Gtk/include/libpng12
LIBS = -LC:/Winware/Gtk/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0
-lgdk_pixbuf-2.0 -lpangowin32-1.0 -lgdi32 -lpangocairo-1.0 -lpango-1.0 -lcairo
-lgobject-2.0 -lgmodule-2.0 -lglib-2.0 -lintl
main:
$(CC) foo.c -o foo.exe $(CFLAGS) $(LIBS)**
然后您可以撰写一个简单的GTK程式:
* foo.c
**#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "哈啰!GTK+!");
gtk_widget_show(window);
gtk_main();
return 0;
}**
为了可以显示中文,您的档案必须以UTF-8存档(档首无BOM),接着执行make指令:
$ make
gcc foo.c -o foo.exe -mms-bitfields -IC:/Winware/Gtk/include/gtk-2.0 -IC:/Winwar
e/Gtk/lib/gtk-2.0/include -IC:/Winware/Gtk/include/atk-1.0 -IC:/Winware/Gtk/incl
ude/cairo -IC:/Winware/Gtk/include/pango-1.0 -IC:/Winware/Gtk/include/glib-2.0 -
IC:/Winware/Gtk/lib/glib-2.0/include -IC:/Winware/Gtk/include/libpng12 -LC:/Winw
are/Gtk/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lpangowi
n32-1.0 -lgdi32 -lpangocairo-1.0 -lpango-1.0 -lcairo -lgobject-2.0 -lgmodule-2.0
-lglib-2.0 -lintl
程式将产生foo.exe,执行后的结果如下所示:
更多GTK的文件,请参考 GTK 学习笔记。。。