Linux 下autotools的使用
在linux环境下,你可能从github上拿到一份代码,只需要执行简单的步骤,就可以完成编译和安装,例如:./configuremakemake install你所执行的configure文件就是由autotools自动产生的,它同时自动产生Makefile文件供你编译和安装。下面,我们来一起学习一下如何使用autotools来编译生成自己的project:构建configure脚本...
在linux环境下,你可能从github上拿到一份代码,只需要执行简单的步骤,就可以完成编译和安装,例如:
./configure
make
make install
你所执行的configure文件就是由autotools自动产生的,它同时自动产生Makefile文件供你编译和安装。
下面,我们来一起学习一下如何使用autotools来编译生成自己的project:
构建 configure脚本 和 Makefile .in
如果是自己的C项目如何构建configure和Makefile.in呢?
答案是autoconf
和 automake
工具
首先 写一个 m4sh 格式的文件 configure.ac
:
1 AC_INIT([hello_dyy], [0.1], [yayongx.duan@.intel.com])
2 AM_INIT_AUTOMAKE
3 AC_PROG_CC
4 AC_CONFIG_FILES([Makefile])
5 AC_OUTPUT
再写一个 Makefile.am
文件:
1 AUTOMAKE_OPTIONS = foreign
2 bin_PROGRAMS = hello_dyy
3 helloworld_SOURCES = hello.c
接下来,先使用 aclocal
工具(aclocal
会自动扫描 configure.ac
),生成 aclocal.m4
文件,这个文件定义了一些 macro 以便后面 automake
工具使用。
命令行输入:
aclocal
autoconf
根据 configure.ac
文件生成 configure
脚本。
在命令行里输入:
autoconf
automake
根据 Makefile.am
文件生成 Makefile.in
文件。
在命令行里输入:
automake --add-missing
安装程序
yayongx@Debug-And-Trace-Server1:~/dyy$ ./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 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 for style of include used by make... GNU
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: executing depfiles commands
yayongx@Debug-And-Trace-Server1:~/dyy$ ls
Makefile Makefile.in autom4te.cache config.log configure depcomp install-sh
Makefile.am aclocal.m4 compile config.status configure.ac hello.c missing
yayongx@Debug-And-Trace-Server1:~/dyy$ make
gcc -DPACKAGE_NAME=\"hello\" -DPACKAGE_TARNAME=\"hello\" -DPACKAGE_VERSION=\"0.1\" -DPACKAGE_STRING=\"hello\ 0.1\" -DPACKAGE_BUGREPORT=\"yayongx.duan@intel.com\" -DPACKAGE_URL=\"\" -DPACKAGE=\"hello\" -DVERSION=\"0.1\" -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_dyy hello.o
yayongx@Debug-And-Trace-Server1:~/dyy$ sudo make install
[sudo] password for yayongx:
make[1]: Entering directory '/home/yayongx/dyy'
/bin/mkdir -p '/usr/local/bin'
/usr/bin/install -c hello_dyy '/usr/local/bin'
make[1]: Nothing to be done for 'install-data-am'.
make[1]: Leaving directory '/home/yayongx/dyy'
yayongx@Debug-And-Trace-Server1:~/dyy$ hello_dyy
hello word!yayongx@Debug-And-Trace-Server1:~/dyy$
项目例子源码
hello.c
1 #include <stdio.h>
2
3 int main()
4 {
5 » printf("hello word");
6 » return 0;
7 }
configure.ac
1 # -*- Autoconf -*-
2 # Process this file with autoconf to produce a configure script.
3
4 AC_INIT([hello], [0.1], [yayongx.duan@intel.com])
5 AM_INIT_AUTOMAKE
6 # Checks for programs.
7 AC_PROG_CC
8 AC_CONFIG_FILES([Makefile])
9
10 # Checks for libraries.
11
12 # Checks for header files.
13
14 # Checks for typedefs, structures, and compiler characteristics.
15
16 # Checks for library functions.
17
18 AC_OUTPUT
Makefile.am
1 AUTOMAKE_OPTIONS = foreign
2 bin_PROGRAMS = hello_dyy
3 hello_dyy_SOURCES = hello.c
补充说明
autoscan: 扫描源代码以搜寻普通的可移植性问题,比如检查编译器,库,头文件等,生成文件configure.scan,
它是configure.ac的一个雏形。
aclocal:根据已经安装的宏,用户定义宏和acinclude.m4文件中的宏将configure.ac文件所需要的宏集中定义到
文件 aclocal.m4中。aclocal是一个perl 脚本程序,它的定义是:“aclocal - create aclocal.m4 by
scanning configure.ac”
automake:将Makefile.am中定义的结构建立Makefile.in,然后configure脚本将生成的Makefile.in文件转换
为Makefile。如果在configure.ac中定义了一些特殊的宏,比如AC_PROG_LIBTOOL,它会调用libtoolize,
否则它 会自己产生config.guess和config.sub
`autoconf:将configure.ac中的宏展开,生成configure脚本。这个过程可能要用到aclocal.m4中定义的宏。
autoreconf :
可以通过简单的autoreconf -fvi命令,直接生成configure,Makefile.in,config.h.in等文件。
./configure 生成Makefile
make 生成执行程序hello
Run autoconf (and autoheader
, aclocal
, automake
, autopoint
(formerly gettextize
), and libtoolize
where appropriate) repeatedly
to remake the GNU Build System files in specified DIRECTORIES and their
subdirectories (defaulting to .
).
configure.ac文件变量简要说明
所有以”#”号开始的行为注释:
AC_PREREQ
宏声明本文件要求的autoconf版本,本例使用的版本为2.59。
AC_INIT
宏用来定义软件的名称和版本等信息,FULL-PACKAGE-NAME
为软件包名称,VERSION
为软件版本号,BUG-REPORT-ADDRESS
为BUG报告地址(一般为软件作者邮件地址)。
AC_CONFIG_SRCDIR
宏用来侦测所指定的源码文件是否存在,来确定源码目录的有效性。此处为当前目录下的hello.c。
AC_CONFIG_HEADER
宏用于生成config.h文件,以便autoheader使用。
AC_PROG_CC
用来指定编译器,如果不指定,选用默认gcc。
AC_OUTPUT
用来设定 configure 所要产生的文件,如果是makefile,configure会把它检查出来的结果带入makefile.in文件产生合适的makefile。使用Automake时,还需要一些其他的参数,这些额外的宏用aclocal工具产生。
Makefile.am文件变量简要说明
AUTOMAKE_OPTIONS
为设置Automake的选项。由于GNU对自己发布的软件有严格的规范,比如必须附带许可证声明文件COPYING
等,否则Automake执行时会报错。Automake提供了3种软件等级:foreign
、gnu
和gnits
,供用户选择,默认等级为gnu。本例使需用foreign
等级,它只检测必须的文件。
bin_PROGRAMS
定义要产生的执行文件名。如果要产生多个执行文件,每个文件名用空格隔开。
hello_SOURCES
定义”hello”这个执行程序所需要的原始文件。如果”hello”这个程序是由多个原始文件所产生的,则必须把它所用到的所有原始文件都列出来,并用空格隔开。例如:若目标体”hello”需要”hello.c”、”hello.h”两个依赖文件,则定义hello_SOURCES=hello.c hello.h
。
更多推荐
所有评论(0)