新闻  |   论坛  |   博客  |   在线研讨会
****学Makefile
xiajiashan | 2012-08-28 15:08:11    阅读:775   发布文章

(第二部分:庞大的makefile是怎么生成的 )

四:自动生成makefile

4.1 以helloworld.c为源文件

root@parson-desktop:/home/parson/tmp/automk# ls

hellolinux.c

4.2.使用Autoscan工具生成configure.ac文件

root@parson-desktop:/home/parson/tmp/automk# autoscan

程序“autoscan”尚未安装。 您可以使用以下命令安装:

apt-get install autoconf

安装autoconf,输入如下命令

root@parson-desktop:/home/parson/tmp/automk# apt-get install autoconf

正在读取软件包列表... 完成

正在分析软件包的依赖关系树

正在读取状态信息... 完成

将会安装下列额外的软件包:

automake autotools-dev

建议安装的软件包:

autoconf2.13 autoconf-archive gnu-standards autoconf-doc libtool gettext

下列【新】软件包将被安装:

autoconf automake autotools-dev

升级了 0 个软件包,新安装了 3 个软件包,要卸载 0 个软件包,有 273 个软件包未被升级。

需要下载 1,444kB 的软件包。

解压缩后会消耗掉 4,325kB 的额外空间。

您希望继续执行吗?[Y/n]y

获取:1 http://ftp.sjtu.edu.cn/ubuntu/ lucid/main autoconf 2.65-3ubuntu1 [772kB]

获取:2 http://ftp.sjtu.edu.cn/ubuntu/ lucid/main autotools-dev 20090611.1 [64.1kB]

获取:3 http://ftp.sjtu.edu.cn/ubuntu/ lucid/main automake 1:1.11.1-1 [608kB]

下载 1,444kB,耗时 2分 59秒 (8,044B/s)

选中了曾被取消选择的软件包 autoconf。

(正在读取数据库 ... 系统当前总共安装有 149752 个文件和目录。)

正在解压缩 autoconf (从 .../autoconf_2.65-3ubuntu1_all.deb) ...

选中了曾被取消选择的软件包 autotools-dev。

正在解压缩 autotools-dev (从 .../autotools-dev_20090611.1_all.deb) ...

选中了曾被取消选择的软件包 automake。

正在解压缩 automake (从 .../automake_1%3a1.11.1-1_all.deb) ...

正在处理用于 man-db 的触发器...

正在处理用于 doc-base 的触发器...

Processing 39 changed 1 added doc-base file(s)...

Registering documents with scrollkeeper...

正在处理用于 install-info 的触发器...

正在设置 autoconf (2.65-3ubuntu1) ...

正在设置 autotools-dev (20090611.1) ...

正在设置 automake (1:1.11.1-1) ...

update-alternatives: 使用 /usr/bin/automake-1.11 来提供 /usr/bin/automake (automake),于 自动模式 中。

root@parson-desktop:/home/parson/tmp/automk# ls

hellolinux.c

再一次执行autoscan

root@parson-desktop:/home/parson/tmp/automk# autoscan

root@parson-desktop:/home/parson/tmp/automk# ls

autoscan.log configure.scan hellolinux.c

生成了autoscan.log和configure.scan

我们看看生成了两个文件是什么东西

root@parson-desktop:/home/parson/tmp/automk# cat autoscan.log

autoscan.log没有内容

root@parson-desktop:/home/parson/tmp/automk# cat configure.scan

# -*- Autoconf -*-

# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.65])

AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])

AC_CONFIG_SRCDIR([helloworld.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_OUTPUT

(1)AC_PREREQ宏声明本文件要求的autoconf版本,本例使用的版本为2.65。

(2)AC_INIT宏用来定义软件的名称和版本等信息,”FULL-PACKAGE-NAME”为软件包名称,”VERSION”为软件版本号,”BUG-REPORT-ADDRESS”为BUG报告地址(一般为软件作者邮件地址)。

(3)AC_CONFIG_SRCDIR宏用来侦测所指定的源码文件是否存在,来确定源码目录的有效性。此处为当前目录下的helloworld.c。

(4)AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader使用。

(5)AC_PROG_CC用来指定编译器,如果不指定,选用默认gcc。

(6)AC_OUTPUT用来设定 configure 所要产生的文件,如果是makefile,configure会把它检查出来的结果带入makefile.in文件产生合适的makefile。使用 Automake时,还需要一些其他的参数,这些额外的宏用aclocal工具产生。

root@parson-desktop:/home/parson/tmp/automk# mv configure.scan configure.ac

root@parson-desktop:/home/parson/tmp/automk# vim configure.ac

# -*- Autoconf -*-

# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.65])

AC_INIT(hellolinux,1.0,somy@xiajiashan.com)

AM_INIT_AUTOMAKE(hellolinux,1.0)

AC_CONFIG_SRCDIR([hellolinux.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_OUTPUT(Makefile)~

"configure.ac" 21L, 498C 已写入

root@parson-desktop:/home/parson/tmp/automk# ls

autoscan.log configure.ac hellolinux.c

4.3 利用aclocal工具制作aclocal.m4文件

root@parson-desktop:/home/parson/tmp/automk# aclocal

root@parson-desktop:/home/parson/tmp/automk# ls

aclocal.m4 autom4te.cache autoscan.log configure.ac hellolinux.c

4.4 利用autoconf制作configure工具

root@parson-desktop:/home/parson/tmp/automk# autoconf

root@parson-desktop:/home/parson/tmp/automk# ls

aclocal.m4 autom4te.cache autoscan.log configure configure.ac hellolinux.c

4.5编写Makefile.am文件

root@parson-desktop:/home/parson/tmp/automk# vim Makefile.am

AUTOMAKE_OPTIONS=foreign

bin_PROGRAMS=hellolinux

hellolinux_SOURCES=hellolinux.c~

"Makefile.am" [新] 3L, 81C 已写入

4.6 通过automake产生Makefile.in文件

root@parson-desktop:/home/parson/tmp/automk# ./configure

configure: error: cannot find install-sh, install.sh, or shtool in "." "./.." "./../.."

root@parson-desktop:/home/parson/tmp/automk# automake --add-missing

configure.ac:6: installing `./install-sh'

configure.ac:6: installing `./missing'

Makefile.am: installing `./depcomp'

root@parson-desktop:/home/parson/tmp/automk# ls

aclocal.m4 autoscan.log configure depcomp install-sh Makefile.in

autom4te.cache config.log configure.ac hellolinux.c Makefile.am missing

4.7 通过运行./configure产生Makefile文件

root@parson-desktop:/home/parson/tmp/automk# ./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 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 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

root@parson-desktop:/home/parson/tmp/automk# ls

aclocal.m4 autoscan.log config.status configure.ac hellolinux.c Makefile Makefile.in

autom4te.cache config.log configure depcomp install-sh Makefile.am missing

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
02年接触ARM和ucos,开发过有线和无线图像报警器,IPCamera,人脸识别系统,OCR识别系统,指纹识别系统,05年开始从事Linux及Rtems下WiFi,camera,Ethernet等驱动开发工作,专做嵌入式linux培训,致力于把我十年来的研发经验传授给每一个学员,招人的可以找我,ximenpiaoxue4016@sina.com
推荐文章
最近访客