教程:使用gnu autotools系列工具構建gtk+應用程序

以一個例子介紹具體步驟:html

測試環境:Win7+MinGW(linux更不用說確定能夠了)linux

1.新建主程序demo.c:shell

#include <gtk/gtk.h>

int main(int argc, char * argv[]){
	GtkWidget * window;
	gtk_init(&argc, &argv);

	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

	g_signal_connect_swapped(G_OBJECT(window), "destroy",
			G_CALLBACK(gtk_main_quit), G_OBJECT(window));

	gtk_widget_show(window);
	gtk_main();

	return 0;
}

2.使用autoscan掃描源代碼,生成configure.scan文件:

./autoscan

生成的configure.scan文件以下:

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.63])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([demo.c])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT

3.將configure.scan重命名爲configure.ac,修改和添加必要的信息,修改後文件以下:

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.63])
AC_INIT([demo], [1.0], [tsl0922@gmail.com])// 軟件名稱、版本號、做者郵箱
AM_INIT_AUTOMAKE(demo, 1.0)                // Automake所必備的宏,必須添加
AC_CONFIG_SRCDIR([demo.c])                 // 源文件名
AC_CONFIG_HEADERS([config.h])          // config文件,爲autoheader準備

# Checks for programs.
AC_PROG_CC// 編譯器,默認GCC

# Checks for libraries.
# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([Makefile])// 指定輸出文件名爲Makefile
AC_OUTPUT

4.

(1)使用aclocal掃描configure.ac,生成aclocal.m4文件:aclocalwindows

(2)使用autoconf工具將configure.ac中的宏展開,生成configure腳本:autoconfapp

(3)使用autoheader生成config.h.in文件:autoheader工具

(4)使用automake生成Makefile.in文件:automake --add-missing學習

合併爲一個腳本:autogen.sh測試

aclocal
autoconf
autoheader
automake --add-missing

5.手工編寫Makefile.am文件:ui

AUTOMAKE_OPTIONS = foreign
INCLUDES = `pkg-config --cflags gtk+-2.0`     //包含gtk庫
LIBS = `pkg-config --libs gtk+-2.0`           //連接gtk庫文件
bin_PROGRAMS = demo
demo_SOURCES = demo.c

6.執行autogen.sh,並運行configure生成Makefile:

./autogen.sh
./configure

7.生成最終程序文件:this

make

最終生成的文件以下:

運行程序:

./demo

運行結果:

使用Automake生成的Makefile文件至關全面,支持的預設命令有:

1.	編譯:make
2.	安裝:make install
3.	卸載:make uninstall
4.	清理:make clean

附:簡單說下windows下gtk+開發環境搭建:

下載MinGW:

我使用的是已經有的Mplayer WW的MinGW編譯環境,也能夠到官網去下載。我用的環境下載地址:

http://sourceforge.net/projects/mplayer-ww/files/MinGW-full/,下載MinGW-full-20101119.7z

下載gtk+:

http://mirrors.ustc.edu.cn/gnome/binaries/win32/gtk+/2.24/,下載gtk+-bundle_2.24.8-20111122_win32.zip

把gtk解壓到一個路徑如C:\gtk,並添加到環境變量中(我是直接把gtk解壓到MinGW目錄中覆蓋相同文件)

打開MinGW命令窗口,執行pkg-config --cflags gtk+-2.0,看下有沒有正確輸出gtk庫的包含路徑,再執行下gtk-demo,看是否是能夠運行,這是一個gtk的示例程序,能夠拿來學習,有源碼的。

參考:http://www.ruchee.com/code/linux/gnu/automake.html

<教程完>

相關文章
相關標籤/搜索