安裝gtk2.0
sudo apt-get install libgtk2.0-dev
檢視 2.x 版本pkg-config --modversion gtk+-2.0 #有可能需要sudo apt-get install pkg-config
檢視是否安裝了gtkpkg-config --list-all | grep gtk
gtk+版的helloworld測試程式
#include <gtk/gtk.h>
int main(int argc, char* argv[])
{
GtkWidget* window;
GtkWidget* label;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
label = gtk_label_new("Hello World.");
gtk_container_add(GTK_CONTAINER(window), label);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
編譯執行
gcc -o helloworld helloworld.c `pkg-config --cflags --libs gtk+-2.0`
./Helloworld
用於編譯gtk程式的Makefile
# a general Makefile to build gtk app in only one src file case.
CC = gcc
CFLAGS =
LDFLAGS = `pkg-config gtk+-2.0 --cflags --libs`
SRCS = $(wildcard *.c)
TARGETS = $(patsubst %.c, %, $(SRCS))
all:$(TARGETS)
$(TARGETS):$(SRCS)
$(CC) $^ -o $@ $(LDFLAGS) $(CFLAGS)
clean:
-rm -rf $(TARGETS) *~ .*swp
.PHONY: clean all
# a general Makefile to build
CC = gcc
CFLAGS =
LDFLAGS = `pkg-config gtk+-2.0 --cflags --libs`
SRCS = test1.c test2.c main.c
TARGETS = app
all:$(TARGETS)
$(TARGETS):$(SRCS)
$(CC) $^ -o $@ $(LDFLAGS) $(CFLAGS)
clean:
-rm -rf $(TARGETS) *~ .*swp
.PHONY: clean all
cygwin下搭建gtk+ 2.0開發環境
到官網http://www.cygwin.com/下載最新的setup.exe,安裝執行到Select Packages的時候,搜尋安裝如下包
libiconv: GNU character set conversion library and utilities
libiconv2: GNU character set conversion library and utilities - runtime(1)
pkg-config: A utility used to retrieve information about installed libraries
zlib-devel: The zlib compression/decompression library (development)
zlib0: The zlib compression/decompression library (runtime)
libpng14: PNG library (1.4 runtime)
libpng14-devel: PNG library (development)
libjpeg-devel: A library for manipulating JPEG image format files (devel)
libjpeg8: A library for manipulating JPEG image format files (runtime)
libtiff-devel: TIFF image library (development)
libtiff5: TIFF image library (runtime)
libglib2.0-devel: GNOME C function library (development)
libglib2.0_0: GNOME C function library (runtime)
libatk1.0-devel: Accessibility Toolkit library (developement)
libatk1.0_0: Accessibility Toolkit library (runtime)
libpango1.0-devel: GNOME text layout and renadering library (development)
libpango1.0_0: GNOME text layout and renadering library (runtime)
libcairo-devel: Cross-device vector graphics library (development)
libcairo: Cross-device vector graphics library (runtime)
libgtk2.0-devel: Multi-platform GUI toolkit (development)
libgtk2.0_0: Multi-platform GUI toolkit (runtime)
gcc-core: C compiler
gcc-g++: C++ compiler
make
vim(非必須)
xinit(xinit這個是最重要的包: X.Org X server initializer)
安裝完成之後,檢查gcc是否安裝成功
$ gcc --version
然後檢查gtk+-2.0的詳細版本號
$ pkg-config gtk+-2.0 --libs
使用之前的helloworld程式編譯測試一下,是否能夠通過,執行的時候如果提示Gtk-WARNING **: cannot open display:
重啟cygwin控制檯,然後執行
$ startxwin
或者在開始選單下,執行Cygwin-X下的XWin Server,然後再啟動應用程式,即可正常執行。