下面的例子是在 linux下创建的一个包含多个源文件的c语言项目,其中基本用到了整个autotools自动化工具链(autoscan,aclocal,autoheader,automake,autoconf,make)。如果读者要制作符合GNU标准的开源项目,这是一个不错的开始。
$ mkdir 1
$ cd 1
$ touch main.c plus.c plus.h minus.c minus.h
$ cat >main.c
#include <stdio.h>
#include "plus.h"
#include "minus.h"
int main(int argc, char **argv)
{
int i0, i1, i2, i3;
i0 = 3;
i1 = 2;
i2 = plus(i0, i1);
i3 = minus(i0, i1);
printf("%d plus %d equal to %d\n", i0, i1, i2);
printf("%d minus %d equal to %d\n", i0, i1, i3);
return 0;
}
$ cat >plus.c
int plus(int i0, int i1)
{
return i0 + i1;
}
$ cat >plus.h
int plus(int i0, int i1);
$ cat >minus.c
int minus(int i0, int i1)
{
return i0 - i1;
}
$ cat >minus.h
int minus(int i0, int i1);
$ autoscan
$ mv configure.scan configure.ac
/
// 在 configure.ac 里面增加两个宏
// AC_CONFIG_FILES([Makefile])
// AM_INIT_AUTOMAKE
// 将 AC_INIT 的值改成下面的形式
// AC_INIT(myprogram, 1.0, support@qq.com)
/
$ vi configure.ac
$ cat >Makefile.am
bin_PROGRAMS = myprogram
myprogram_SOURCES = main.c plus.c plus.h minus.c minus.h
$ aclocal
$ touch NEWS AUTHORS README ChangeLog
$ autoheader
$ automake --add-missing
$ autoconf
$ ./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... gawk
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: creating config.h
config.status: executing depfiles commands
$ make
make all-am
make[1]: Entering directory `/home/nbz/1'
gcc -DHAVE_CONFIG_H -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 -DHAVE_CONFIG_H -I. -g -O2 -MT plus.o -MD -MP -MF .deps/plus.Tpo -c -o plus.o plus.c
mv -f .deps/plus.Tpo .deps/plus.Po
gcc -DHAVE_CONFIG_H -I. -g -O2 -MT minus.o -MD -MP -MF .deps/minus.Tpo -c -o minus.o minus.c
mv -f .deps/minus.Tpo .deps/minus.Po
gcc -g -O2 -o myprogram main.o plus.o minus.o
make[1]: Leaving directory `/home/nbz/1'
$ ls
aclocal.m4 autoscan.log config.h.in configure depcomp main.c
AUTHORS ChangeLog config.log configure.ac INSTALL main.o
autom4te.cache config.h config.status COPYING install-sh Makefile
$ ./myprogram
3 plus 2 equal to 5
3 minus 2 equal to 1
$ make dist
{ test ! -d "myprogram-1.0" || { find "myprogram-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr "myprogram-1.0"; }; }
test -d "myprogram-1.0" || mkdir "myprogram-1.0"
test -n "" \
|| find "myprogram-1.0" -type d ! -perm -755 \
-exec chmod u+rwx,go+rx {} \; -o \
! -type d ! -perm -444 -links 1 -exec chmod a+r {} \; -o \
! -type d ! -perm -400 -exec chmod a+r {} \; -o \
! -type d ! -perm -444 -exec /bin/sh /home/nbz/1/install-sh -c -m a+r {} {} \; \
|| chmod -R a+r "myprogram-1.0"
tardir=myprogram-1.0 && /bin/sh /home/nbz/1/missing --run tar chof - "$tardir" | GZIP=--best gzip -c >myprogram-1.0.tar.gz
{ test ! -d "myprogram-1.0" || { find "myprogram-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr "myprogram-1.0"; }; }
$ ls
aclocal.m4 ChangeLog config.status depcomp main.o minus.c myprogram plus.h
AUTHORS config.h configure INSTALL Makefile minus.h myprogram-1.0.tar.gz plus.o
autom4te.cache config.h.in configure.ac install-sh Makefile.am minus.o NEWS README
autoscan.log config.log COPYING main.c Makefile.in missing plus.c stamp-h1
$ make dist-zip
{ test ! -d "myprogram-1.0" || { find "myprogram-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr "myprogram-1.0"; }; }
test -d "myprogram-1.0" || mkdir "myprogram-1.0"
test -n "" \
|| find "myprogram-1.0" -type d ! -perm -755 \
-exec chmod u+rwx,go+rx {} \; -o \
! -type d ! -perm -444 -links 1 -exec chmod a+r {} \; -o \
! -type d ! -perm -400 -exec chmod a+r {} \; -o \
! -type d ! -perm -444 -exec /bin/sh /home/nbz/1/install-sh -c -m a+r {} {} \; \
|| chmod -R a+r "myprogram-1.0"
rm -f myprogram-1.0.zip
zip -rq myprogram-1.0.zip myprogram-1.0
{ test ! -d "myprogram-1.0" || { find "myprogram-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr "myprogram-1.0"; }; }
$ ls
aclocal.m4 ChangeLog config.status depcomp main.o minus.c myprogram plus.c stamp-h1
AUTHORS config.h configure INSTALL Makefile minus.h myprogram-1.0.tar.gz plus.h
autom4te.cache config.h.in configure.ac install-sh Makefile.am minus.o myprogram-1.0.zip plus.o
autoscan.log config.log COPYING main.c Makefile.in missing NEWS README
$