automake:从c文件到make

automake:

如果你在Linux上从源代码安装过软件,一定记得./configure、make、make install的安装命令。这个神奇的configure和make是什么生成的,下面就将一步一步实现。

首先你有一个C文件,这里将它命名为abc.c。我们将它制作成一个名叫abc的软件包,可以使用./configure、make、make install安装到Linux系统中。

/**
 * abc.c 这是一个简单的程序
 * 用来演示automake是怎么样
 * 一步一步开始工作的
 */
#include <stdio.h>

int main(int argc, char* argv[]){
	printf("hello world!\n");
	return 0
}

我的系统中当前安装的软件有:

sys-devel/autoconf-2.13:2.1
sys-devel/autoconf-2.69:2.5
sys-devel/automake-1.9.6-r3:1.9
sys-devel/automake-1.11.6:1.11
sys-devel/libtool-2.4.2:2

sys-devel/make-3.82-r4
sys-devel/m4-1.4.16
sys-apps/texinfo-4.13-r2
app-arch/tar-1.27.1-r2

图出自wikipedia

mkdir abc
cd abc
have abc.c

$autoscan
$ls
abc.c autoscan-2.69.log configure.scan

$cp configure.scan configure.ac
$aclocal
$ls
abc.c autom4te.cache/ autoscan-2.69.log configure.ac configure.scan

$vim configure.ac
AC_INIT([...
>>AC_INIT([abc], [0.0.1], [lophyxp@163.com])
AM_INIT_AUTOMAKE([foreign])
$aclocal
$ls
abc.c aclocal.m4 autom4te.cache/ autoscan-2.69.log configure.ac configure.scan

$autoheader
$ls
abc.c aclocal.m4 autom4te.cache/ autoscan-2.69.log config.h.in configure.ac configure.scan

$autoconf
$ls
abc.c aclocal.m4 autom4te.cache/ autoscan-2.69.log config.h.in configure* configure.ac 

configure.scan

$automake
configure.ac:6: error: required file './install-sh' not found
configure.ac:6:   'automake --add-missing' can install 'install-sh'
configure.ac:6: error: required file './missing' not found
configure.ac:6:   'automake --add-missing' can install 'missing'
automake-1.13: error: no 'Makefile.am' found for any configure output
$./configure
configure: error: cannot find install-sh, install.sh, or shtool in "." "./.." "./../.."

$automake --add-missing
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
automake-1.13: error: no 'Makefile.am' found for any configure output

$touch Makefile.am
$automake --add-missing
automake-1.13: error: no 'Makefile.am' found for any configure output
automake-1.13: Did you forget AC_CONFIG_FILES([Makefile]) in configure.ac?

$vim configure.ac
AC_OUTPUT
>>AC_CONFIG_FILES([Makefile])
AC_OUTPUT
$autoconf
$ls
abc.c aclocal.m4 autom4te.cache/ autoscan-2.69.log config.h.in configure* configure.ac 
configure.scan install-sh Makefile.am missing

$automake --add-missing
$ls
abc.c aclocal.m4 autom4te.cache/ autoscan-2.69.log config.h.in config.log configure* configure.ac 
configure.scan install-sh Makefile.am Makefile.in missing

$./configure
$ls
abc.c aclocal.m4 autom4te.cache/ autoscan-2.69.log config.h config.h.in config.log config.status 
configure* configure.ac configure.scan install-sh Makefile.am Makefile.in missing stamp-h1

$make
***
***
***

$make
make  all-am
make[1]: 进入目录“/home/lophyxp/abc”
make[1]: 离开目录“/home/lophyxp/abc”
$ls
abc.c aclocal.m4 autom4te.cache/ autoscan-2.69.log config.h config.h.in config.log config.status 
configure* configure.ac configure.scan install-sh Makefile Makefile.am Makefile.in missing stamp-h1

$echo ************************************************
$echo Now configure ready!
$echo ************************************************

$vim Makefile.am
bin_PROGRAMS=abc
abc_SOURCES=abc.c

$automake
Makefile.am: error: required file './depcomp' not found
Makefile.am:   'automake --add-missing' can install 'depcomp'
$ls
abc.c aclocal.m4 autom4te.cache/ autoscan-2.69.log config.h config.h.in config.log config.status 
configure* configure.ac configure.scan install-sh Makefile Makefile.am Makefile.in missing stamp-h1

$automake --add-missing
Makefile.am: installing './depcomp'
$ls
abc.c aclocal.m4 autom4te.cache/ autoscan-2.69.log config.h config.h.in config.log config.status 
configure* configure.ac configure.scan depcomp install-sh Makefile Makefile.am Makefile.in 
missing stamp-h1

$echo ************************************************
$echo Now Makefile.am ready!
$echo ************************************************

$./configure
$make
make  all-am
make[1]: 进入目录“/home/lophyxp/abc”
gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT abc.o -MD -MP -MF .deps/abc.Tpo -c -o abc.o abc.c
mv -f .deps/abc.Tpo .deps/abc.Po
gcc  -g -O2   -o abc abc.o
make[1]: 离开目录“/home/lophyxp/abc”

练习:

1、touch一个空的Makefile.am文件,不在里面填写任何内容,最后看看出现什么情况。

2、编辑configure.ac时候不添加AC_CONFIG_FILES([Makefile]),最后看看出现什么情况。

3、编辑configure.ac时候不添加AM_INIT_AUTOMAKE([foreign]),最后看看出现什么情况。


万能的autoreconf

如果已经有了编写好并测试过的configure.ac和Makefile.am,手工按顺序运行各个auto命令实在很繁琐。这时候你可以用autoreconf:

$autoreconf -v --install
一切都会按合理地顺序执行妥当。然后你就可以./configure、make、make install了。


关于自动打包

./configure后,make dist可以将当前软件自动打包。现在的Linux上的软件包多为tar.gz、tar.bzip2或者tar.xz格式。所以自动打包也支持很多压缩格式:

$fgrep dist- Makefile
DIST_TARGETS = dist-gzip
dist-gzip: distdir
dist-bzip2: distdir
dist-lzip: distdir
dist-xz: distdir
dist-tarZ: distdir
dist-shar: distdir
dist-zip: distdir
dist dist-all:
        cscopelist-am ctags ctags-am dist dist-all dist-bzip2 \
        dist-gzip dist-lzip dist-shar dist-tarZ dist-xz dist-zip \


关于交叉编译

./configure使用 --host参数,如果存在交叉编译器,可以进行交叉编译。

$./configure --host mips64el-unkown-linux-gnu
$make
$file abc
abc: ELF 32-bit LSB executable, MIPS, N32 MIPS-III version 1 (SYSV), dynamically linked, interpreter /lib32/ld.so.1, for GNU/Linux 2.6.32, not stripped

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值