1.配置gcc
sudo apt-get install build-essential
2.安装gtk包
sudo apt-get install vim #使用vim来编写代码,当然您可以使用任何自己喜欢的编辑器
sudo apt-get install build-essential #这将安装gcc/g /gdb/make 等基本编程工具
sudo apt-get install gnome-core-devel #这将安装 libgtk2.0-dev libglib2.0-dev 等开发相关的库文件
sudo apt-get install pkg-config #用于在编译GTK程序时自动找出头文件及库文件位置
sudo apt-get install devhelp #这将安装 devhelp GTK文档查看程序
sudo apt-get install libglib2.0-doc libgtk2.0-doc #这将安装 gtk/glib 的API参考手册及其它帮助文档
sudo apt-get instal glade libglade2-dev #这将安装基于GTK的界面构造程序。
主要是sudo apt-get install gnome-core-devel 这一步
3.测试
#include<gtk/gtk.h>
void hello(GtkWidget *widget,gpointer data)
{
g_print("Hello Ubuntu!/n");
}
gint delete_event(GtkWidget *widget,GdkEvent *event,gpointer data)
{
g_print ("delete event occurred/n");
return(TRUE);
}
void destroy(GtkWidget *widget,gpointer data)
{
gtk_main_quit();
}
int main( int argc, char *argv[] )
{
GtkWidget *window;
GtkWidget *button;
gtk_init (&argc, &argv);
window=gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_signal_connect (GTK_OBJECT(window),"delete_event",GTK_SIGNAL_FUNC(delete_event),NULL);
gtk_signal_connect (GTK_OBJECT (window), "destroy",GTK_SIGNAL_FUNC (destroy), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
button = gtk_button_new_with_label ("Hello Ubuntu!");
gtk_signal_connect (GTK_OBJECT (button), "clicked",GTK_SIGNAL_FUNC (hello), NULL);
gtk_signal_connect_object (GTK_OBJECT (button), "clicked",GTK_SIGNAL_FUNC (gtk_widget_destroy),GTK_OBJECT (window));
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (button);
gtk_widget_show (window);
gtk_main();
return(0);
}
用下面命令编译运行
$ gcc gtkhello.c -o gtktest `pkg-config --cflags --libs gtk -2.0`
$ ./gtktest
会显示一个带有一个按钮的窗口,点击按钮以后窗口关闭,命令行显示Hello Ubuntu。
对应的makefile:
#makefile for test
gtktest:gtktest.c
gcc -o gtktest gtktest.c `pkg-config --cflags --libs gtk+-2.0`
clean:
rm -f gtktest *.o