使用automake编写Makefile文件:GTK+篇

首先,不免俗地编写一个简单的GTK+程序gtkhello.c:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
* gtkhello.c
*
* Created on: 2010-1-28
* Author: young
*/
#include <gtk/gtk.h>
int main(int argc, char *argv[])
{
GtkWidget *wnd;
GtkWidget *label;
gtk_init(&argc, &argv);
wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(wnd), "Hello GTK+!");
gtk_window_set_default_size(GTK_WINDOW(wnd), 300, 200);
label = gtk_label_new("Hello, welcome to the GTK+ world in Ubuntu 9.10\n\n(C) 2010 Chinsoft Studio");
gtk_container_add(GTK_CONTAINER(wnd), label);
g_signal_connect(GTK_OBJECT(wnd), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(wnd);
gtk_main();
return 0;
}

接下来,我们开始使用automake等命令编写Makefile文件。

(1) 在终端中运行autoscan命令;

1
$ ls -l
总计 8
-rw-r--r-- 1 young young   0 2010-01-28 21:35 autoscan.log
-rw-r--r-- 1 young young 470 2010-01-28 21:35 configure.scan
-rw-r--r-- 1 young young 720 2010-01-28 21:35 gtkhello.c

(2) 使用mv命令将onfigure.scan重命名为configure.ac或configure.in并修改文件内容:

修改前文件:

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

AC_PREREQ([2.64])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([gtkhello.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

修改后文件:

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

AC_PREREQ([2.64])
AC_INIT(gtkhello, 1.10.01, yyhoo2.young@gmail.com)
AC_CONFIG_SRCDIR([gtkhello.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE(gtkhello,1.10.01)

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.
AM_PATH_GTK_2_0(2.2.0,,AC_MSG_ERROR(gtkhello 0.1 needs GTK+ 2.2.0))

AC_OUTPUT(Makefile)

(3) 在当前目录下新建Makefile.am文件,文件内容如下:

bin_PROGRAMS = gtkhello
gtkhello_SOURCES = gtkhello.c
AM_CPPFLAGS  = @GTK_CFLAGS@
LDADD = @GTK_LIBS@

(4) 在当前目录下使用touch命令新建NEWS、 README、 ChangeLog 、AUTHORS 四个文件,否则运行automake –add-missing命令时将会报错;

(5) 在终端中依次运行以下命令:

1
2
3
4
$ aclocal
$ autoheader
$ autoconf
$ automake --add-missing

(6)如果没有任何错误,继续运行./configure命令:

1
$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables... 
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking for pkg-config... /usr/bin/pkg-config
checking for GTK+ - version >= 2.2.0... yes (version 2.18.3)
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands

即生成Makefile文件。

(7) 使用make命令即可编译gtkhello.c程序。

参考资料: