autoconf与automake使用简解

总结

以hello.c为例介绍了C文件如何使用autoconf和automake工具自动生成Makefile

1. 安装automake,创建hello.c文件

sudo apt-get install automake

最好是在一个空文件夹下创建

kjay@kjay:~/Makefile_Tutorial/hello$ ls
hello.c
kjay@kjay:~/Makefile_Tutorial/hello$ cat hello.c 
#include <stdio.h> 
int main()
{
    printf("Hello, Linux World!\n");  
    return 0;  
}

2. 使用autoscan自动检查hello.c编译所需的文件等

kjay@kjay:~/Makefile_Tutorial/hello$ autoscan
kjay@kjay:~/Makefile_Tutorial/hello$ ls
autoscan.log  configure.scan  hello.c

使用autoscan --help查看autoscan的作用

Examine source files in the directory tree rooted at SRCDIR, or the
current directory if none is given. Search the source files for
common portability problems, check for incompleteness of
“configure.ac”, and create a file “configure.scan” which is a
preliminary “configure.ac” for that package.

在源文件中检查有没有常见的可移植问题,检查configure.ac的完整性,如果没有configure.ac则创建configure.scan文件,configure.scan需要在之后重命名为configure.ac并做改动

kjay@kjay:~/Makefile_Tutorial/hello$ mv configure.scan configure.ac
kjay@kjay:~/Makefile_Tutorial/hello$ cat configure.ac 
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.69])
AC_INIT([hello], [1.0], [xxx@qq.com])
AM_INIT_AUTOMAKE # Add, automake
AC_CONFIG_SRCDIR([hello.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_CONFIG_FILES([Makefile]) # Add, Create Makefile

AC_OUTPUT

AM_INIT_AUTOMAKE提供automake所需参数,在GNU手册中,对于hello.c这样比较简单的情况,直接写在AC_INIT后就可以了

AM_INIT_AUTOMAKE

AC_OUTPUT的作用为

the AC_OUTPUT line is a closing command that actually produces the part of the script in charge of creating the files registered with AC_CONFIG_HEADERS and AC_CONFIG_FILES.

用于生成AC_CONFIG_HEADERS 和 AC_CONFIG_FILES定义的文件,这里定义Makefile,用于后续自动生成

3. 执行aclocal,生成aclocal.m4 配置文件

kjay@kjay:~/Makefile_Tutorial/hello$ aclocal
kjay@kjay:~/Makefile_Tutorial/hello$ ls
aclocal.m4  autom4te.cache  autoscan.log  configure.ac  hello.c

输入aclocal --help,可以看到它的作用:

Generate “aclocal.m4” by scanning “configure.ac” or “configure.in”

根据configure.ac或者configure.in生成aclocal.m4文件,它会扫描configure.ac文件中的宏定义,并加入aclocal.m4中

4. 执行autoconf,生成configure文件

kjay@kjay:~/Makefile_Tutorial/hello$ autoconf
kjay@kjay:~/Makefile_Tutorial/hello$ ls
aclocal.m4  autom4te.cache  autoscan.log  configure  configure.ac  hello.c

输入autoconf --help,可以看到它的作用

Generate a configuration script from a TEMPLATE-FILE if given, or configure.ac if present, or else configure.in. Output is sent to the standard output if TEMPLATE-FILE is given, else into configure.

根据configure.ac生成configure文件

5. 执行autoheader,生成config.h.in

config.h.in是config.h的前置文件

kjay@kjay:~/Makefile_Tutorial/hello$ autoheader
kjay@kjay:~/Makefile_Tutorial/hello$ ls
aclocal.m4  autom4te.cache  autoscan.log  config.h.in  configure  configure.ac  hello.c

输入autoheader --help,可以看到它的作用

Create a template file of C #define statements for configure to use. To this end, scan TEMPLATE-FILE, or configure.ac if present, or else configure.in.

扫描configure.ac生成config.h.in

6. 创建Makefile.am文件,写入相关内容

kjay@kjay:~/Makefile_Tutorial/hello$ cat Makefile.am 
AUTOMAKE_OPTION=foreign
bin_PROGRAMS=hello
hello_SOURCES=hello.c

Makefile.am中存放着生成makefile的一系列指令

automake的严格等级为foreign,只检测必须的文件

以_PROGRAMS结尾的变量值是最后make时生成的可执行文件的名字,在这里是hello

bin_PROGRAMS代表最后可执行文件会被装在bin目录下,这里是/usr/local/bin,不同机器可能不一样

当执行automake时,会将.am文件的所有行复制到Makefile.in中,具体细节可以再查看GNU手册

7. 根据实际情况创建常用文档文件

kjay@kjay:~/Makefile_Tutorial/hello$ touch NEWS README AUTHORS ChangeLog
kjay@kjay:~/Makefile_Tutorial/hello$ ls
aclocal.m4  AUTHORS  autom4te.cache  autoscan.log  ChangeLog  config.h.in  configure  configure.ac  hello.c  Makefile.am  NEWS  README

不创建这些后续执行configure的时候会报错的 : )

8. 执行automake,生成Makefile.in

kjay@kjay:~/Makefile_Tutorial/hello$ automake --add-missing
configure.ac:11: installing './compile'
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
Makefile.am: installing './INSTALL'
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
Makefile.am: installing './depcomp'

automake的功能是

Generate Makefile.in for configure from Makefile.am.

–add-missing参数的作用是

add missing standard files to package

9. 执行./configure生成最终的Makefile

kjay@kjay:~/Makefile_Tutorial/hello$ ./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... /usr/bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
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 whether gcc understands -c and -o together... yes
checking whether make supports the include directive... yes (GNU style)
checking dependency style of gcc... gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands
kjay@kjay:~/Makefile_Tutorial/hello$ ls
aclocal.m4  autom4te.cache  ChangeLog  config.h     config.log     configure     COPYING  hello.c  install-sh  Makefile.am  missing  README
AUTHORS     autoscan.log    compile    config.h.in  config.status  configure.ac  depcomp  INSTALL  Makefile    Makefile.in  NEWS     stamp-h1

PS: 使用**./configure --help**的时候可以看到自己设置的信息被添加在configure里了

10. 运行make和make install

kjay@kjay:~/Makefile_Tutorial/hello$ sudo make
make  all-am
make[1]: Entering directory '/home/kjay/Makefile_Tutorial/hello'
gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT hello.o -MD -MP -MF .deps/hello.Tpo -c -o hello.o hello.c
mv -f .deps/hello.Tpo .deps/hello.Po
gcc  -g -O2   -o hello hello.o  
make[1]: Leaving directory '/home/kjay/Makefile_Tutorial/hello'
kjay@kjay:~/Makefile_Tutorial/hello$ ls
aclocal.m4  autom4te.cache  ChangeLog  config.h     config.log     configure     COPYING  hello    hello.o  install-sh  Makefile.am  missing  README
AUTHORS     autoscan.log    compile    config.h.in  config.status  configure.ac  depcomp  hello.c  INSTALL  Makefile    Makefile.in  NEWS     stamp-h1
kjay@kjay:~/Makefile_Tutorial/hello$ sudo make install
make[1]: Entering directory '/home/kjay/Makefile_Tutorial/hello'
 /usr/bin/mkdir -p '/usr/local/bin'
  /usr/bin/install -c hello '/usr/local/bin'
make[1]: Nothing to be done for 'install-data-am'.
make[1]: Leaving directory '/home/kjay/Makefile_Tutorial/hello'

make生成hello可执行文件,make install将hello可执行文件Install到/usr/local/bin中

kjay@kjay:~/Makefile_Tutorial/hello$ ./hello 
Hello, Linux World!
kjay@kjay:~/Makefile_Tutorial/hello$ hello
Hello, Linux World!

11. 一图流总结

在这里插入图片描述

参考

GNU automake手册

GNU autoconf手册

autoconf automake使用

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值