Linux下搭建gtk+2.0开发环境

时间:2022-02-13 07:33:31
安装gtk2.0
sudo apt-get install libgtk2.0-dev

查看 2.x 版本
pkg-config --modversion gtk+-2.0 #有可能需要sudo apt-get install pkg-config

查看是否安装了gtk
pkg-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,然后再启动应用程序,即可正常运行。