新闻中心

EEPW首页>嵌入式系统>设计应用> 自动生成 Makefile 的全过程详解

自动生成 Makefile 的全过程详解

作者: 时间:2016-10-10 来源:网络 收藏

4 、新建Makefile.am

新建Makefile.am 文件,命令:

$ vi Makefile.am

内容如下:

AUTOMAKE_OPTIONS=foreign

bin_PROGRAMS=helloworld

helloworld_SOURCES=helloworld.c

automake 会根据你写的Makefile.am 来自动生成Makefile.in 。

Makefile.am 中定义的宏和目标, 会指导automake 生成指定的代码。例如,宏bin_PROGRAMS 将导致编译和连接的目标被生成。

5 、运行automake

命令:

$ automake --add-missing

configure.in: installing `./install-sh'

configure.in: installing `./mkinstalldirs'

configure.in: installing `./missing'

Makefile.am: installing `./depcomp'

automake 会根据Makefile.am 文件产生一些文件,包含最重要的Makefile.in 。

6 、执行configure 生成Makefile

$ ./configure

checking for a BSD-compatible install... /usr/bin/install -c

checking whether build environment is sane... yes

checking for gawk... gawk

checking whether make sets $(MAKE)... yes

checking for gcc... gcc

checking for C compiler default output... 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 ANSI C... 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

$ ls -l Makefile

-rw-rw-r-- 1 yutao yutao 15035 Oct 15 10:40 Makefile

你可以看到,此时Makefile 已经产生出来了。

7 、使用Makefile 编译代码

$ make

if gcc -DPACKAGE_NAME= -DPACKAGE_TARNAME= -DPACKAGE_VERSION= -

DPACKAGE_STRING= -DPACKAGE_BUGREPORT= -DPACKAGE=helloworld -DVERSION=1.0

-I. -I. -g -O2 -MT helloworld.o -MD -MP -MF .deps/helloworld.Tpo

-c -o helloworld.o `test -f 'helloworld.c' || echo './'`helloworld.c;

then mv -f .deps/helloworld.Tpo .deps/helloworld.Po;

else rm -f .deps/helloworld.Tpo; exit 1;

fi

gcc -g -O2 -o helloworld helloworld.o

运行helloworld

$ ./helloworld

Hello, Linux World!

这样helloworld 就编译出来了,你如果按上面的步骤来做的话,应该也会很容易地编译出正确的helloworld 文件。你还可以试着使用一些其 他的make 命令,如make clean ,make install ,make dist ,看看它们会给你什么样的效果。感觉如何?自己也能写出这么专业的Makefile ,老板一定会对你刮目相看。

四、深入浅出

针对上面提到的各个命令,我们再做些详细的介绍。

1 、 autoscan

autoscan 是用来扫描源代码目录生成configure.scan 文件的。autoscan 可以用目录名做为参数,但如果你不使用参数的话,那么 autoscan 将认为使用的是当前目录。autoscan 将扫描你所指定目录中的源文件,并创建configure.scan 文件。

2 、 configure.scan

configure.scan 包含了系统配置的基本选项,里面都是一些宏定义。我们需要将它改名为configure.in

3 、 aclocal

aclocal 是一个perl 脚本程序。aclocal 根据configure.in 文件的内容,自动生成aclocal.m4 文件。aclocal 的定义是:“aclocal - create aclocal.m4 by scanning configure.ac” 。

4 、 autoconf

autoconf 是用来产生configure 文件的。configure 是一个脚本,它能设置源程序来适应各种不同的操作系统平台,并且根据不同的系统来产生合适的Makefile ,从而可以使你的源代码能在不同的操作系统平台上被编译出来。

configure.in 文件的内容是一些宏,这些宏经过autoconf 处理后会变成检查系统特性、环境变量、软件必须的参数的shell 脚本。configure.in 文件中的宏的顺序并没有规定,但是你必须在所有宏的最前 面和最后面分别加上AC_INIT 宏和AC_OUTPUT 宏。

在configure.ini 中:

# 号表示注释,这个宏后面的内容将被忽略。

AC_INIT(FILE)

这个宏用来检查源代码所在的路径。

AM_INIT_AUTOMAKE(PACKAGE, VERSION)

这个宏是必须的,它描述了我们将要生成的软件包的名字及其版本号:PACKAGE 是软件包的名字,VERSION 是版本号。当你使用make dist 命令时,它会给你生成一个类似helloworld-1.0.tar.gz 的软件发行包,其中就有对应的软件包的名字和版本号。

AC_PROG_CC

这个宏将检查系统所用的C 编译器。

AC_OUTPUT(FILE)

这个宏是我们要输出的Makefile 的名字。

我们在使用automake 时,实际上还需要用到其他的一些宏,但我们可以用aclocal 来帮我们自动产生。执行aclocal后我们会得到aclocal.m4 文件。

产生了configure.in 和aclocal.m4 两个宏文件后,我们就可以使用autoconf 来产生configure 文件了。

5 、 Makefile.am

Makefile.am 是用来生成Makefile.in 的,需要你手工书写。Makefile.am 中定义了一些内容:

AUTOMAKE_OPTIONS

这个是automake 的选项。在执行automake 时,它会检查目录下是否存在标准GNU 软件包中应具备的各种文件,例如AUTHORS 、ChangeLog 、NEWS 等文件。我们将其设置成foreign 时,automake 会改用一般软件包的标准来检查。



关键词:linux

评论


相关推荐

技术专区

关闭