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