Makefile、configure是如何生成的

本文深入探讨了自动化构建工具的工作流程,包括使用autoscan、aclocal、autoheader、automake、autoconf等工具从源代码生成配置脚本和Makefile的过程。通过实际案例,详细展示了如何从简单的源代码文件开始,逐步构建起复杂的软件项目结构。

在这里插入图片描述

1.autoscan (autoconf): 扫描源代码以搜寻普通的可移植性问题,比如检查编译器,库,头文件等,生成文件configure.scan,它是configure.ac的一个雏形。 your source files --> [autoscan*] --> [configure.scan] --> configure.ac
2.aclocal (automake):根据已经安装的宏,用户定义宏和acinclude.m4文件中的宏将configure.ac文件所需要的宏集中定义到文件 aclocal.m4中。aclocal是一个perl 脚本程序,它的定义是:“aclocal - create aclocal.m4 by scanning configure.ac”

在这里插入图片描述

3.autoheader(autoconf): 根据configure.ac中的某些宏,比如cpp宏定义,运行m4,生成config.h.in

在这里插入图片描述

4.automake: automake将Makefile.am中定义的结构建立Makefile.in,然后configure脚本将生成的Makefile.in文件转换 为Makefile。如果在configure.ac中定义了一些特殊的宏,比如AC_PROG_LIBTOOL,它会调用libtoolize,否则它 会自己产生config.guess和config.sub

在这里插入图片描述

5.autoconf:将configure.ac中的宏展开,生成configure脚本。这个过程可能要用到aclocal.m4中定义的宏。

在这里插入图片描述

6.configure的过程 在这里插入图片描述
7. make过程

在这里插入图片描述

8实例:

在 helloword目录下创建一个hello.c文件,并编译运行它:
#cd helloworld/
编写源文件hello.c:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}
sundaweideMBP:helloworld sundawei$ ll
total 24
drwxr-xr-x  4 sundawei  staff   128  5 12 14:02 ./
drwxr-xr-x  7 sundawei  staff   224  5 12 13:53 ../
-rw-r--r--@ 1 sundawei  staff  6148  5 12 14:02 .DS_Store
-rw-r--r--  1 sundawei  staff    46  5 12 13:54 hello.c

一、autoscan

sundaweideMBP:helloworld sundawei$ autoscan
sundaweideMBP:helloworld sundawei$ ll
total 32
drwxr-xr-x  6 sundawei  staff   192  5 12 15:18 ./
drwxr-xr-x  7 sundawei  staff   224  5 12 13:53 ../
-rw-r--r--@ 1 sundawei  staff  6148  5 12 15:17 .DS_Store
-rw-r--r--  1 sundawei  staff     0  5 12 15:18 autoscan.log
-rw-r--r--  1 sundawei  staff   467  5 12 15:18 configure.scan
-rw-r--r--  1 sundawei  staff    46  5 12 13:54 hello.c

已经生成了configure.scan,autoscan.log文件
将configure.scan 修改为 configure.ac,最后修改的内容如下:

sundaweideMBP:helloworld sundawei$  mv configure.scan configure.ac  
sundaweideMBP:helloworld sundawei$ vim configure.ac
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.69])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC
# 在制作makefile的过程中,执行aclocal命令,发现只生成了一个autom4te.cache文件夹,并没生成aclocal.m4这个文件。
# 原因是在configure.ac文件中缺少了宏的引用。
AM_INIT_AUTOMAKE
# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT

二、aclocal
aclocal ,生成 aclocal.m4 和 autom4te.cache (生成aclocal.m4的过程中涉及到configure.in)

sundaweideMBP:helloworld sundawei$ aclocal
sundaweideMBP:helloworld sundawei$ ll
total 120
drwxr-xr-x  8 sundawei  staff    256  5 12 15:21 ./
drwxr-xr-x  7 sundawei  staff    224  5 12 13:53 ../
-rw-r--r--@ 1 sundawei  staff   6148  5 12 15:17 .DS_Store
-rw-r--r--  1 sundawei  staff  41882  5 12 15:21 aclocal.m4
drwxr-xr-x  5 sundawei  staff    160  5 12 15:21 autom4te.cache/
-rw-r--r--  1 sundawei  staff      0  5 12 15:18 autoscan.log
-rw-r--r--  1 sundawei  staff    689  5 12 15:21 configure.ac
-rw-r--r--  1 sundawei  staff     46  5 12 13:54 hello.c

三、autoconf
autoconf,生成 configure (根据 configure.in, 和 aclocal.m4)

sundaweideMBP:helloworld sundawei$ autoconf
sundaweideMBP:helloworld sundawei$ ll
total 392
drwxr-xr-x  9 sundawei  staff     288  5 12 15:21 ./
drwxr-xr-x  7 sundawei  staff     224  5 12 13:53 ../
-rw-r--r--@ 1 sundawei  staff    6148  5 12 15:17 .DS_Store
-rw-r--r--  1 sundawei  staff   41882  5 12 15:21 aclocal.m4
drwxr-xr-x  7 sundawei  staff     224  5 12 15:21 autom4te.cache/
-rw-r--r--  1 sundawei  staff       0  5 12 15:18 autoscan.log
-rwxr-xr-x  1 sundawei  staff  139086  5 12 15:21 configure*
-rw-r--r--  1 sundawei  staff     689  5 12 15:21 configure.ac
-rw-r--r--  1 sundawei  staff      46  5 12 13:54 hello.c

四、编写Makefile.am:

AUTOMAKE_OPTIONS= foreign
bin_PROGRAMS= hello
hello_SOURCES= hello.c

五、automake
:在执行automake --add-missing之前执行autoheader,生成config.h.in

sundaweideMBP:helloworld sundawei$ autoheader
sundaweideMBP:helloworld sundawei$ ll
total 424
drwxr-xr-x  11 sundawei  staff     352  5 12 15:31 ./
drwxr-xr-x   7 sundawei  staff     224  5 12 13:53 ../
-rw-r--r--@  1 sundawei  staff    6148  5 12 15:30 .DS_Store
-rw-r--r--   1 sundawei  staff      69  5 12 15:22 Makefile.am
-rw-r--r--   1 sundawei  staff   41882  5 12 15:30 aclocal.m4
drwxr-xr-x   7 sundawei  staff     224  5 12 15:30 autom4te.cache/
-rw-r--r--   1 sundawei  staff       0  5 12 15:18 autoscan.log
-rw-r--r--   1 sundawei  staff     625  5 12 15:31 config.h.in
-rwxr-xr-x   1 sundawei  staff  146163  5 12 15:30 configure*
-rw-r--r--   1 sundawei  staff     699  5 12 15:24 configure.ac
-rw-r--r--   1 sundawei  staff      46  5 12 13:54 hello.c

生成 Makefile.in, depcomp, install-sh, 和 missing (根据 Makefile.am, 和 aclocal.m4)

sundaweideMBP:helloworld sundawei$ automake
configure.ac:10: error: required file './compile' not found
configure.ac:10:   'automake --add-missing' can install 'compile'
configure.ac:14: error: required file './install-sh' not found
configure.ac:14:   'automake --add-missing' can install 'install-sh'
configure.ac:14: error: required file './missing' not found
configure.ac:14:   'automake --add-missing' can install 'missing'
Makefile.am: error: required file './depcomp' not found
Makefile.am:   'automake --add-missing' can install 'depcomp'
sundaweideMBP:helloworld sundawei$ automake --add-missing
configure.ac:10: installing './compile'
configure.ac:14: installing './install-sh'
configure.ac:14: installing './missing'
Makefile.am: installing './depcomp'
sundaweideMBP:helloworld sundawei$ ll
total 472
drwxr-xr-x  16 sundawei  staff     512  5 12 15:32 ./
drwxr-xr-x   7 sundawei  staff     224  5 12 13:53 ../
-rw-r--r--@  1 sundawei  staff    6148  5 12 15:30 .DS_Store
-rw-r--r--   1 sundawei  staff      69  5 12 15:22 Makefile.am
-rw-r--r--   1 sundawei  staff   24152  5 12 15:32 Makefile.in
-rw-r--r--   1 sundawei  staff   41882  5 12 15:30 aclocal.m4
drwxr-xr-x   7 sundawei  staff     224  5 12 15:32 autom4te.cache/
-rw-r--r--   1 sundawei  staff       0  5 12 15:18 autoscan.log
lrwxr-xr-x   1 sundawei  staff      38  5 12 15:32 compile@ -> /usr/local/share/automake-1.16/compile
-rw-r--r--   1 sundawei  staff     625  5 12 15:31 config.h.in
-rwxr-xr-x   1 sundawei  staff  146163  5 12 15:30 configure*
-rw-r--r--   1 sundawei  staff     699  5 12 15:24 configure.ac
lrwxr-xr-x   1 sundawei  staff      38  5 12 15:32 depcomp@ -> /usr/local/share/automake-1.16/depcomp
-rw-r--r--   1 sundawei  staff      46  5 12 13:54 hello.c
lrwxr-xr-x   1 sundawei  staff      41  5 12 15:32 install-sh@ -> /usr/local/share/automake-1.16/install-sh
lrwxr-xr-x   1 sundawei  staff      38  5 12 15:32 missing@ -> /usr/local/share/automake-1.16/missing

六、configure
生成 Makefile, config.log, 和 config.status

sundaweideMBP:helloworld sundawei$ ./configure 
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 for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... ./install-sh -c -d
checking for gawk... no
checking for mawk... no
checking for nawk... no
checking for awk... awk
checking whether make sets $(MAKE)... yes
checking whether make supports the include directive... yes (GNU style)
checking whether make supports nested variables... yes
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
sundaweideMBP:helloworld sundawei$ ll
total 624
drwxr-xr-x  22 sundawei  staff     704  5 12 15:34 ./
drwxr-xr-x   7 sundawei  staff     224  5 12 13:53 ../
-rw-r--r--@  1 sundawei  staff    6148  5 12 15:30 .DS_Store
drwxr-xr-x   3 sundawei  staff      96  5 12 15:34 .deps/
-rw-r--r--   1 sundawei  staff   24231  5 12 15:34 Makefile
-rw-r--r--   1 sundawei  staff      69  5 12 15:22 Makefile.am
-rw-r--r--   1 sundawei  staff   24152  5 12 15:32 Makefile.in
-rw-r--r--   1 sundawei  staff   41882  5 12 15:30 aclocal.m4
drwxr-xr-x   7 sundawei  staff     224  5 12 15:32 autom4te.cache/
-rw-r--r--   1 sundawei  staff       0  5 12 15:18 autoscan.log
lrwxr-xr-x   1 sundawei  staff      38  5 12 15:32 compile@ -> /usr/local/share/automake-1.16/compile
-rw-r--r--   1 sundawei  staff     824  5 12 15:34 config.h
-rw-r--r--   1 sundawei  staff     625  5 12 15:31 config.h.in
-rw-r--r--   1 sundawei  staff    9454  5 12 15:34 config.log
-rwxr-xr-x   1 sundawei  staff   32757  5 12 15:34 config.status*
-rwxr-xr-x   1 sundawei  staff  146163  5 12 15:30 configure*
-rw-r--r--   1 sundawei  staff     699  5 12 15:24 configure.ac
lrwxr-xr-x   1 sundawei  staff      38  5 12 15:32 depcomp@ -> /usr/local/share/automake-1.16/depcomp
-rw-r--r--   1 sundawei  staff      46  5 12 13:54 hello.c
lrwxr-xr-x   1 sundawei  staff      41  5 12 15:32 install-sh@ -> /usr/local/share/automake-1.16/install-sh
lrwxr-xr-x   1 sundawei  staff      38  5 12 15:32 missing@ -> /usr/local/share/automake-1.16/missing
-rw-r--r--   1 sundawei  staff      23  5 12 15:34 stamp-h1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值