為了搞清linux下SWT與GTK+的映射機制,翻出老筆記本裝了個Ubuntu,配置GTK+開發環境還是比較簡單的。
- 安裝必須要的東西
- 安裝JDK,使用命令:sudo apt-get install sun-java6-sdk
- 安裝eclipse+CDT 5.0,我覺得CDT從5開始漸成氣候了,作為一個eclipse開發者,我覺得他比kdevelop好用:),遺憾的是在win下不支持msvc,只能通過mingw或cygwin
- 安裝gnome和gtk+開發庫,使用命令:sudo apt-get install libgtk2.0-dev libgnome2-dev ,安裝之后,所有相關的頭文件和靜態/動態庫文件已經準備好了,不得不說apt-get真NB。
- 測試一下
- 頭文件使用命令 pkg-config --cflags gtk+-2.0,正常情況下會出現一下結果:
-I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/freetype2 -I/usr/include/libpng12 -I/usr/include/pixman-1
- 庫文件使用命令 pkg-config --libs gtk+-2.0,正常情況下會出現一下結果:
-lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lm -lpangocairo-1.0 -lpango-1.0 -lcairo -lgobject-2.0 -lgmodule-2.0 -ldl -lglib-2.0
- 啟動Eclipse,新建一個c project,然后右鍵單擊 properties->c/c++ build->Settings,在Tool Setting中將給GCC設置頭文件目錄以及鏈接庫(還有一種做法是自定義make file,里面調用pkg-config,因為沒有顯式設置,cdt無法做靜態分析,不能利用它的強大功能,比如索引,重構等)。
- 設置頭文件,點擊GCC C Compiler->Directories,然后使用命令 pkg-config --cflags gtk+-2.0 | sed 's/ /\n/g' > include.txt,然后使用命令 sed 's/-I//g' include.txt,依次將出現的每一行add到inclue path里面去。
- 設置庫,點擊GCC C Linker->Libraries,然后如頭文件操作一樣使用命令pkg-config --libs gtk+-2.0 | sed 's/ /\n/g' > lib.txt,然后使用命令 sed 's/-l//g' lib.txt,依次將出現的每一行添加到libraries里。
- 編寫一個包含GTK+測試代碼的C文件,添加到項目里面,內容如下:
#include <gtk/gtk.h>
static void destroy(GtkWidget*, gpointer);
static gboolean delete_event(GtkWidget*, GdkEvent*, gpointer);
int main(int argc, char *argv[]) {
GtkWidget *window, *label;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW (window), "Hello World!");
gtk_container_set_border_width(GTK_CONTAINER (window), 10);
gtk_widget_set_size_request(window, 200, 100);
/* Connect the main window to the destroy and delete-event signals. */
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);
g_signal_connect(G_OBJECT(window), "delete_event",
G_CALLBACK(delete_event), NULL);
/* Create a new GtkLabel widget that is selectable. */
label = gtk_label_new("Hello World");
gtk_label_set_selectable(GTK_LABEL (label), TRUE);
/* Add the label as a child widget of the window. */
gtk_container_add(GTK_CONTAINER (window), label);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
/* Stop the GTK+ main loop function when the window is destroyed. */
static void destroy(GtkWidget *window, gpointer data) {
gtk_main_quit();
}
/* Return FALSE to destroy the widget. By returning TRUE, you can cancel
* a delete-event. This can be used to confirm quitting the application. */
static gboolean delete_event(GtkWidget *window, GdkEvent *event, gpointer data) {
return FALSE;
}
(以上代碼來自圖書 Foundation of GTK+ development)
然后運行,正常會顯式如下:

Ubuntu真是個好東西,耗的資源少,圖形系統穩定,對開發人員及其友好,真的很強大。
|