1. 没有makefile的日子
2. 参考资料及代码下载
<1>. 没有makefile的日子
[上一篇]中介绍了makefile的编写规则,第一个标题是“没有makefile的日子”,收到[博客园]的网友zhuangzhuang1988的回复可以使用automake来自动生成makefile。如果在linux下从源代码安装过程序的话,通常的过程是这样:
./configure; make; make install
下面我们将试图如何生成这种发行版本,这里面主要使用到了aclocal, automake等工具。
1. 现在假设程序源代码已经编写完成,这里是main.c
xuqiang@ubuntu:~/automake/helloworld$ ls
main.c
xuqiang@ubuntu:~/automake/helloworld$ cat main.c
#include
int main()
{
printf("hello linux world !");
return 0;
}
2. 运行autoscan,此时将生成autoscan.log,configure.scan两个文件。
xuqiang@ubuntu:~/automake/helloworld$ autoscan
xuqiang@ubuntu:~/automake/helloworld$ ls
autoscan.log configure.scan main.c
3. 我们将 configure.scan作为configure.in文件的模板,以供aclocal来使用。首先将configure.scan改名为configure.in
xuqiang@ubuntu:~/automake/helloworld$ mv configure.scan configure.in
修改configure.in件:
xuqiang@ubuntu:~/automake/helloworld$ vim configure.in
xuqiang@ubuntu:~/automake/helloworld$ cat configure.in
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.63])
AC_CONFIG_SRCDIR([main.c])
# comment this line, no use
# 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.
# init automake
AM_INIT_AUTOMAKE(helloauto, 1.0)
# write out the makefile
AC_OUTPUT(Makefile)
4. 执行aclocal命令,生成aclocal.m4 autom4te.cache两个文件
xuqiang@ubuntu:~/automake/helloworld$ aclocal
xuqiang@ubuntu:~/automake/helloworld$ ls
aclocal.m4 autom4te.cache autoscan.log configure.in main.c
5. 运行autoconf命令,将生成configure文件。
xuqiang@ubuntu:~/automake/helloworld$ autoconf
xuqiang@ubuntu:~/automake/helloworld$ ls
aclocal.m4 autom4te.cache autoscan.log configure configure.in main.c
6. 新建Makefile.am文件,该文件是工具automake的依赖文件。
xuqiang@ubuntu:~/automake/helloworld$ vim Makefile.am
xuqiang@ubuntu:~/automake/helloworld$ cat Makefile.am
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=helloauto
helloauto_SOURCES=main.c
7. 运行automake命令,该命令将在该源文件目录中增加所需脚本,例如INSTALL文件等。
xuqiang@ubuntu:~/automake/helloworld$ automake --add-missing
Makefile.am: installing `./INSTALL'
Makefile.am: required file `./NEWS' not found
Makefile.am: required file `./README' not found
Makefile.am: required file `./AUTHORS' not found
Makefile.am: required file `./ChangeLog' not found
Makefile.am: installing `./COPYING' using GNU General Public License v3 file
Makefile.am: Consider adding the COPYING file to the version control system
Makefile.am: for your code, to avoid questions about which license your project uses.
xuqiang@ubuntu:~/automake/helloworld$ ls
aclocal.m4 autoscan.log config.status configure.in INSTALL Makefile.am
autom4te.cache config.log configure COPYING main.c Makefile.in
8. 下面开始测试一下上面生成的文件。
xuqiang@ubuntu:~/automake/helloworld$ ./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
configure: creating ./config.status
config.status: creating Makefile
config.status: executing depfiles commands
xuqiang@ubuntu:~/automake/helloworld$
make
gcc -DPACKAGE_NAME=\"helloauto\" -DPACKAGE_TARNAME=\"helloauto\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"helloauto\ 1.0\" -DPACKAGE_BUGREPORT=\"
[email protected]\" -DPACKAGE=\"helloauto\" -DVERSION=\"1.0\" -I. -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
mv -f .deps/main.Tpo .deps/main.Po
gcc -g -O2 -o helloauto main.o
xuqiang@ubuntu:~/automake/helloworld$
ls
aclocal.m4 config.log configure.in INSTALL Makefile
autom4te.cache config.status COPYING main.c Makefile.am
autoscan.log configure helloauto main.o Makefile.in
xuqiang@ubuntu:~/automake/helloworld$
./helloauto
hello linux world !xuqiang@ubuntu:~/automake/helloworld$
通过使用这些工具,只要编写简单的几个文件就能生成Makefile,编译源代码。
<2>. 参考资料及代码下载
IBM 例解 autoconf 和 automake 生成 Makefile 文件
Linux下Makefile的automake生成全攻略
A tutorial for porting to autoconf & automake
/Files/xuqiang/helloautomake.rar