automake 之 helloworld 实例[转]

来源:互联网 发布:决策树算法原理 编辑:程序博客网 时间:2024/06/14 15:55

# automake --version
automake (GNU automake) 1.10

# autoscan --version
autoscan (GNU Autoconf) 2.59

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1: hello.c 源程序
       #include <stdio.h>
       int main(){
               printf("Hello world!");
               printf("\n");
      }
2: 在当前目录下,运行 autoscan 命令;
       会生成 configure.scan 文件,
       #mv configure.scan configure.ac

note:
Autoconf 2.50用configure.ac
Autoconf 2.13用configure.in

       文件内容为:
      #                                               -*- Autoconf -*-
      # Process this file with autoconf to produce a configure script.

    AC_PREREQ(2.59)
      AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
      AC_CONFIG_SRCDIR([hello.c])
      AC_CONFIG_HEADER([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
      修改为:
      #                                               -*- Autoconf -*-
      # Process this file with autoconf to produce a configure script.

      AC_PREREQ(2.59)
      AC_INIT(hello.c)                         #changed
      AC_CONFIG_SRCDIR([hello.c])
      AM_INIT_AUTOMAKE(hello,1.0)     #added
      AC_CONFIG_HEADER([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.a
      AC_CONFIG_FILES([Makefile])             #added
      AC_OUTPUT
3: 编写Makefile.am文件:

   AUTOMAKE_OPTIONS=foreign
     bin_PROGRAMS=hello
     hello_SOURCES=hello.c
4: 运行 aclocal 命令.生成 aclocal.m4 文件
5: 运行 autoconf 命令,生成 configure 文件
6: 运行 autoheader 命令,生成 config.h.in 文件 
7: 运行 automake --add-missing 命令,形成 
    install-sh
    missing
    depcomp
    三个文件
8:运行 automake 命令
9:./configure;make ;make install

----------------------------------OVER-----------------
[src = http://hi.baidu.com/fiber212121/blog/item/44aa252ace91b53f5243c1ab.html ]


Log :

EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
hyang0:~/udisk/automake$ mkdir hello
hyang0:~/udisk/automake$ cd hello/
hyang0:~/udisk/automake/hello$ ls
hyang0:~/udisk/automake/hello$ vi hello.c
hyang0:~/udisk/automake/hello$ autoscan 
hyang0:~/udisk/automake/hello$ ls
autoscan.log configure.scan hello.c
hyang0:~/udisk/automake/hello$ cp configure.scan configure.ac
hyang0:~/udisk/automake/hello$ aclocal
hyang0:~/udisk/automake/hello$ ls
aclocal.m4      autoscan.log configure.scan Makefile.am
autom4te.cache configure.ac hello.c
hyang0:~/udisk/automake/hello$ vi autom4te.cache/
output.0 requests traces.0 
hyang0:~/udisk/automake/hello$ autoconf 
hyang0:~/udisk/automake/hello$ ls
aclocal.m4      autoscan.log configure.ac    hello.c
autom4te.cache configure     configure.scan Makefile.am
hyang0:~/udisk/automake/hello$ ls configure*
configure configure.ac configure.scan
hyang0:~/udisk/automake/hello$ autoheader
hyang0:~/udisk/automake/hello$ ls config*
config.h.in configure configure.ac configure.scan
hyang0:~/udisk/automake/hello$ automake --add-missing
configure.ac:7: installing `./missing'
configure.ac:7: installing `./install-sh'
Makefile.am: installing `./depcomp'
hyang0:~/udisk/automake/hello$ ls
aclocal.m4      config.h.in   configure.scan install-sh   missing
autom4te.cache configure     depcomp         Makefile.am
autoscan.log    configure.ac hello.c         Makefile.in
hyang0:~/udisk/automake/hello$ cd ..
hyang0:~/udisk/automake$ ls
automake1.10-1.10+nogfdl            automake1.10_1.10+nogfdl-1.dsc        hello
automake1.10_1.10+nogfdl-1.diff.gz automake1.10_1.10+nogfdl.orig.tar.gz
hyang0:~/udisk/automake$ cp -a hello/ hello-bak
hyang0:~/udisk/automake$ ls hello-bak/
aclocal.m4      config.h.in   configure.scan install-sh   missing
autom4te.cache configure     depcomp         Makefile.am
autoscan.log    configure.ac hello.c         Makefile.in
hyang0:~/udisk/automake$ cd hello-bak/
hyang0:~/udisk/automake/hello-bak$ ls
aclocal.m4      config.h.in   configure.scan install-sh   missing
autom4te.cache configure     depcomp         Makefile.am
autoscan.log    configure.ac hello.c         Makefile.in
hyang0:~/udisk/automake/hello-bak$ automake
hyang0:~/udisk/automake/hello-bak$ ls
aclocal.m4      config.h.in   configure.scan install-sh   missing
autom4te.cache configure     depcomp         Makefile.am
autoscan.log    configure.ac hello.c         Makefile.in
hyang0:~/udisk/automake/hello-bak$ ls Makefile.*
Makefile.am Makefile.in
hyang0:~/udisk/automake/hello-bak$ ./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... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output file name... 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 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: creating config.h
config.status: executing depfiles commands
hyang0:~/udisk/automake/hello-bak$ make
make all-am
make[1]: Entering directory `/home/hyang0/udisk/automake/hello-bak'
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/hyang0/udisk/automake/hello-bak'
hyang0:~/udisk/automake/hello-bak$ make -n
make all-am
make[1]: Entering directory `/home/hyang0/udisk/automake/hello-bak'
make[1]: Nothing to be done for `all-am'.
make[1]: Leaving directory `/home/hyang0/udisk/automake/hello-bak'
hyang0:~/udisk/automake/hello-bak$ 

EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

附另一篇:

1、建目录

在你的工作目录下建一个helloworld目录,我们用它来存放helloworld程序及相关文件,如在/home/my/build下:

$ mkdir helloword
$ cd helloworld

2、 helloworld.c

然后用你自己最喜欢的编辑器写一个hellowrold.c文件,如命令:vi helloworld.c。使用下面的代码作为helloworld.c的内容。

int main(int argc, char** argv)
{
printf("Hello, Linux World!\n");
return 0;
}

完成后保存退出。

现在在helloworld目录下就应该有一个你自己写的helloworld.c了。

3、生成configure

我们使用autoscan命令来帮助我们根据目录下的源代码生成一个configure.in的模板文件。

命令:

$ autoscan
$ ls
configure.scan helloworld.c

执行后在hellowrold目录下会生成一个文件:configure.scan,我们可以拿它作为configure.in的蓝本。

现在将configure.scan改名为configure.in,并且编辑它,按下面的内容修改,去掉无关的语句:

============================configure.in内容开始=========================================
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_INIT(helloworld.c)
AM_INIT_AUTOMAKE(helloworld, 1.0)

# 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.in内容结束=========================================

然后执行命令aclocal和autoconf,分别会产生aclocal.m4及configure两个文件:

$ aclocal
$ls
aclocal.m4 configure.in helloworld.c
$ autoconf
$ ls
aclocal.m4 autom4te.cache configure configure.in helloworld.c


大家可以看到configure.in内容是一些宏定义,这些宏经autoconf处理后会变成检查系统特性、环境变量、软件必须的参数的shell脚本。

autoconf 是用来生成自动配置软件源代码脚本(configure)的工具。configure脚本能独立于autoconf运行,且在运行的过程中,不需要用户的干预。

要生成configure文件,你必须告诉autoconf如何找到你所用的宏。方式是使用aclocal程序来生成你的aclocal.m4。

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

autoconf从configure.in这个列举编译软件时所需要各种参数的模板文件中创建configure。

autoconf需要GNU m4宏处理器来处理aclocal.m4,生成configure脚本。

m4是一个宏处理器。将输入拷贝到输出,同时将宏展开。宏可以是内嵌的,也可以是用户定义的。除了可以展开宏,m4还有一些内建的函数,用来引用文件,执行命令,整数运算,文本操作,循环等。m4既可以作为编译器的前端,也可以单独作为一个宏处理器。

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会改用一般软件包的标准来检查。

bin_PROGRAMS

这个是指定我们所要产生的可执行文件的文件名。如果你要产生多个可执行文件,那么在各个名字间用空格隔开。

helloworld_SOURCES

这个是指定产生“helloworld”时所需要的源代码。如果它用到了多个源文件,那么请使用空格符号将它们隔开。比如需要 helloworld.h,helloworld.c那么请写成helloworld_SOURCES= helloworld.h helloworld.c。

如果你在bin_PROGRAMS定义了多个可执行文件,则对应每个可执行文件都要定义相对的filename_SOURCES。

6、 automake

我们使用automake --add-missing来产生Makefile.in。

选项--add-missing的定义是“add missing standard files to package”,它会让automake加入一个标准的软件包所必须的一些文件。

我们用automake产生出来的Makefile.in文件是符合GNU Makefile惯例的,接下来我们只要执行configure这个shell 脚本就可以产生合适的 Makefile 文件了。

7、 Makefile

在符合GNU Makefiel惯例的Makefile中,包含了一些基本的预先定义的操作:

make

根据Makefile编译源代码,连接,生成目标文件,可执行文件。

make clean

清除上次的make命令所产生的object文件(后缀为“.o”的文件)及可执行文件。

make install

将编译成功的可执行文件安装到系统目录中,一般为/usr/local/bin目录。

make dist

产生发布软件包文件(即distribution package)。这个命令将会将可执行文件及相关文件打包成一个tar.gz压缩的文件用来作为发布软件的软件包。

它会在当前目录下生成一个名字类似“PACKAGE-VERSION.tar.gz”的文件。PACKAGE和VERSION,是我们在configure.in中定义的AM_INIT_AUTOMAKE(PACKAGE, VERSION)。

make distcheck

生成发布软件包并对其进行测试检查,以确定发布包的正确性。这个操作将自动把压缩包文件解开,然后执行configure命令,并且执行make,来确认编译不出现错误,最后提示你软件包已经准备好,可以发布了。

===============================================
helloworld-1.0.tar.gz is ready for distribution
===============================================
make distclean

类似make clean,但同时也将configure生成的文件全部删除掉,包括Makefile。

五、结束语

通过上面的介绍,你应该可以很容易地生成一个你自己的符合GNU惯例的Makefile文件及对应的项目文件。

如果你想写出更复杂的且符合惯例的Makefile,你可以参考一些开放代码的项目中的configure.in和Makefile.am文件,比如:嵌入式数据库sqlite,单元测试cppunit。

[ src = http://hi.baidu.com/vctianya/blog/item/4add83ec606ba62462d09f1b.html ]

另一篇:

在开始使用autoconf和automake之前,首先确认你的系统安装有GNU的如下软件:
1. automake
2. autoconf
3. m4
4. perl
5. 如果你需要产生共享库(shared library)则还需要GNU Libtool

步骤解释如下:
1、由你的源文件通过autoscan命令生成configure.scan文件,然后修改configure.scan文件并重命名为configure.in
2、由aclocal命令生成aclocal.m4
3、由autoconf命令生成configure
4、编辑一个Makefile.am文件并由automake命令生成Makefile.in文件
5、运行configure命令生成Makefile

automake支持三种目录层次:flat、shallow和deep。
一个flat包指的是所有文件都在一个目录中的包。为这类包提供的Makefile.am不需要SUBDIRS这个宏。这类包的一个例子是termutils。对应咱们程序员来说:就是所有源文件及自己写的头文件都位于当前目录里面,且没有子目录。
一个deep包指的是所有的源代码都被储存在子目录中的包;顶层目录主要包含配置信息。GNU cpio是这类包的一个很好的例子,GNU tar也是。deep包的顶层Makefile.am将包括宏SUBDIRS,但没有其它定义需要创建的对象的宏。对应咱们程序员来说:就是所有源文件及自己写的头文件都位于当前目录的一个子目录里面,而当前目录里没有任何源文件。
一个shallow包指的是主要的源代码储存在顶层目录中,而各个部分(典型的是库)则储存在子目录中的包。automake本身就是这类包(GNU make也是如此,它现在已经不使用automake)。对应咱们程序员来说:就是主要源文件在当前目录里,而其它一些实现各部分功能的源文件各自位于不同目录。

前两个层次的程序编辑方法非常简单,按照上述步骤一步步即可。而第三种层次shallow稍微复杂一点,但这是我们经常写程序用到的结构。下面以一个例子说明shallow层次结构的源文件如何自动生成Makefile文件。
例子源程序结构如下:
hello是我们的工作目录,hello目录下有main.c源文件和comm、tools、db、network、interface等五个目录。 comm目录下有comm.c和comm.h源文件及头文件,tools目录下有tools.c和tools.h,同样其它目录分别有db.c、 db.h、network.c、network.h、interface.c、interface.h等一些源文件。

按照如下步骤来自动生成Makefile吧:
1、进入hello目录,运行autoscan命令,命令如下:
cd hello
autoscan
2、ls会发现多了一个configure.scan文件。修改此文件,在AC_INIT宏之后加入AM_INIT_AUTOMAKE(hello, 1.0),这里hello是你的软件名称,1.0是版本号,即你的这些源程序编译将生成一个软件hello-1.0版。然后把 configure.scan文件的最后一行AC_OUTPUT宏填写完整变成AC_OUTPUT(Makefile),表明autoconf和 automake最终将生成Makefile文件。最后把configure.scan文件改名为configure.in。最终 configure.in文件内容如下:

# Process this file with autoconf to produce a configure script.
AC_INIT(target.c)
AM_INIT_AUTOMAKE(hello, 1.0)
# 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)

3、运行aclocal命令,ls会发现多了一个aclocal.m4文件。
4、然后运行autoconf命令,ls将发现生成了一个可执行的configure命令。
5、编辑一个Makefile.am文件,文件内容如下:
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=hello
hello_SOURCES=main.c comm/comm.c comm/comm.h tools/tools.c tools/tools.h db/db.c db/db.h network/network.c network/network.h interface/interface.c interface/interface.h
这表明你最后将通过一个make命令利用上述hello_SOURCES源文件生成一个hello的程序。
6、运行automake --add-missing命令。屏幕提示如下:
automake: configure.in: installing `./install-sh'
automake: configure.in: installing `./mkinstalldirs'
automake: configure.in: installing `./missing'
7、然后你可以运行之前生成的configure命令来生成一个Makefile文件,输入./configure命令即可。
8、编辑Makefile文件,找到$(LINK)所在的那一行,本来生成的文件内容如下:
@rm -f hello
$(LINK) $(hello_LDFLAGS) $(hello_OBJECTS) $(hello_LDADD) $(LIBS)
在这两行之间增加几行变成: 
@rm -f hello
@mv -f comm.o comm
@mv -f tools.o tools
@mv -f db.o db
@mv -f network.o network
@mv -f interface.o interface
$(LINK) $(hello_LDFLAGS) $(hello_OBJECTS) $(hello_LDADD) $(LIBS)
这是因为默认生成的Makefile将在编译后把所有目标文件置于当前目录,而在进行链接(link)时又会到各个子目录去找相应的目标文件。
当然,为了完整,建议各位在clean部分加上如下一些行:
@rm -f comm/comm.o
@rm -f tools/tools.o
@rm -f db/db.o
@rm -f network/network.o
@rm -f interface/interface.o

好了,经过上述这些步骤后,现在你可以来编译生成你自己的可执行程序了。输入一个make all吧,然后就可以运行./hello来看你的程序运行了。

运用autoconf和automake的最大好处是,你的程序以源程序方式发布后,其它所有人只需要依次输入
./configure
make
make install
命令就可以把你的程序安装在自己的电脑上运行了。所有符合GNU标准的UNIX/Linux都不需要再修改Makefile里的任何字符。

[ src = http://hi.baidu.com/vctianya/blog/item/dd1a1c7a14118fee2e73b3c8.html ]

原创粉丝点击