1. 程式人生 > >一個簡單的執行程序的GNU automake自動生成Makefile的方法及案例

一個簡單的執行程序的GNU automake自動生成Makefile的方法及案例

rect -o 創建 otool 其中 ner markdown ted head

一個簡單的執行程序的GNU automake自動生成Makefile的方法及案例

在GNU的世界裏,存在Automake這樣的工具進行自動生成Makefile文件,automake是由Perl語言編寫的,必須與GNU autoconf一並使用,具體的生成過程請參看GNU automake的wikipedia中的右下角的圖,地址如下:http://en.wikipedia.org/wiki/Automake,由此圖可看到使用自動生成Makefile的工具使用的流程,步驟主要如下:

  1. autoscan
  2. 修改生成的configure.scan為configure.in
  3. aclocal
  4. autoheader
  5. autoconf
  6. 創建Makefile.am並進行具體內容的寫入
  7. automake
  8. automake
  9. ./configure生成Makefile
  10. make得到可執行程序

光空說太抽象了,那麽來一個簡單的例子吧,

0. 創建一個printf("Hello world!\n")的小程序,創建目錄hello後創建hello.c,

ufo@ufo:~/hello$ ls
hello.c

那麽下一步即可開始automake的工作了,

1、使用autoscan生成configure.scan

ufo@ufo:~/hello$ autoscan 
ufo@ufo:~/hello$ ls
autoscan.log  configure.scan  hello.c
ufo@ufo:~/hello$ aclocal
aclocal: `configure.ac‘ or `configure.in‘ is required

2、在上一步中直接執行aclocal時出現以上的提示,那麽就要將生成的configure.scan修改為configure.ac或configure.in再進行aclocal的執行;

ufo@ufo:~/hello$ mv configure.scan configure.in 
ufo@ufo:~/hello$ ls
autoscan.log  configure.in  hello.c

3、執行aclocal

ufo@ufo:~/hello$ aclocal
ufo@ufo:~/hello$ ls
autom4te.cache  autoscan.log  configure.in  hello.c

4、執行autoheader

ufo@ufo:~/hello$ ls
autom4te.cache  autoscan.log  config.h.in  configure.in  hello.c

5、執行autoconf

ufo@ufo:~/hello$ autoconf 
ufo@ufo:~/hello$ ls
autom4te.cache  autoscan.log  config.h.in  configure  configure.in  hello.c

6、創建Makefile.am

ufo@ufo:~/hello$ vim Makefile.am 
ufo@ufo:~/hello$ cat Makefile.am 
bin_PROGRAMS=hello
hello_SOURCES=hello.c

關於Makefile.am中的具體內容的意思是說生成的可執行文件的名稱為hello,對應的源代碼為hello.c。

7、執行automake

ufo@ufo:~/hello$ automake
configure.in: no proper invocation of AM_INIT_AUTOMAKE was found.
configure.in: You should verify that configure.in invokes AM_INIT_AUTOMAKE,
configure.in: that aclocal.m4 is present in the top-level directory,
configure.in: and that aclocal.m4 was recently regenerated (using aclocal).
automake: no `Makefile.am‘ found for any configure output
automake: Did you forget AC_CONFIG_FILES([Makefile]) in configure.in?

這時出錯了,是說configure.in文件中的AM_INIT_AUTOMAKE沒有找到,只有修改configure.in
文件後再從第三步進行重新執行,configure.in中的AC_INIT行下添加
AM_INIT_AUTOMAKE(hello,1.0),格式為AM_INIT_AUTOMAKE(package,version),再修改AC_OUTPUT為AC_OUTPUT(Makefile);

修改完configure.in文件後,再次執行2~7;

8、執行automake

ufo@ufo:~/hello$ automake
configure.in:6: required file `./install-sh‘ not found
configure.in:6:   `automake --add-missing‘ can install `install-sh‘
configure.in:6: required file `./missing‘ not found
configure.in:6:   `automake --add-missing‘ can install `missing‘
Makefile.am: required file `./INSTALL‘ not found
Makefile.am:   `automake --add-missing‘ can install `INSTALL‘
Makefile.am: required file `./NEWS‘ not found
Makefile.am: required file `./README‘ not found
Makefile.am: required file `./AUTHORS‘ not found
Makefile.am: required file `./ChangeLog‘ not found
Makefile.am: required file `./COPYING‘ not found
Makefile.am:   `automake --add-missing‘ can install `COPYING‘
Makefile.am: required file `./depcomp‘ not found
Makefile.am:   `automake --add-missing‘ can install `depcomp‘

按照提示創建缺少的文件,

ufo@ufo:~/hello$ touch NEWS README AUTHORS ChangeLog

再執行:

ufo@ufo:~/hello$ automake --add-missing

沒有出錯的情況下再次執行automake;

ufo@ufo:~/hello$ ls
aclocal.m4      ChangeLog     configure.in  INSTALL      missing
AUTHORS         config.h.in   COPYING       install-sh   NEWS
autom4te.cache  config.h.in~  depcomp       Makefile.am  README
autoscan.log    configure     hello.c       Makefile.in

此時已經生成了生成Makefile文件的cinfigure腳本;

9、./configure生成Makefile

ufo@ufo:~/hello$ ls Makefile
Makefile

10、make得到可執行程序

ufo@ufo:~/hello$ make
make  all-am
make[1]: 正在進入目錄 `/home/ufo/hello‘
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]:正在離開目錄 `/home/ufo/hello‘
ufo@ufo:~/hello$ ls
aclocal.m4      config.h       configure     hello.c     Makefile.am  stamp-h1
AUTHORS         config.h.in    configure.in  hello.o     Makefile.in
autom4te.cache  config.h.in~   COPYING       INSTALL     missing
autoscan.log    config.log     depcomp       install-sh  NEWS
ChangeLog       config.status  hello         Makefile    README
ufo@ufo:~/hello$ ./hello 
Hello World!

這就是牛X的automake的例子;關於其中的細節日後有空再表了 ;-)

Ref:

http://en.wikipedia.org/wiki/Automake
http://www.gnu.org/software/automake/manual/
http://socgsa.cs.clemson.edu/seminar/tools06/resources/08_autotools/automake.htm

一個簡單的執行程序的GNU automake自動生成Makefile的方法及案例