Makefile自動產生工具-----autotools的使用(詳細)

來源:互聯網
上載者:User

標籤:style   class   blog   http   tar   ext   

相信每個學習Linux的人都知道Makefile,這是一個很有用的東西,但是編寫它是比較複雜,今天介紹一個它的自動產生工具,autotools的使用。很多GNULinux的的軟體都是用它產生Makefile的,包括我們非常熟悉的Linux核心原始碼。

 

  1、準備:

  需要工具

  autoscan

  aclocal

  autoheader 

  automake

  autoconf

  auto make 

  在終端敲入命令,哪個沒有安裝哪個,一般是第一個autoscan沒有,其它的我用的Ubuntu10.04下全部都有

 

  2、測試程式編寫:
     建立目錄:mkdir include src

     編寫程式:include/str.h

 

[cpp] view plaincopy 
  1. #include <stdio.h>  
  2. int str(char *string);  


    編寫程式:src/str.c

[cpp] view plaincopy 
  1. #include "str.h"  
  2. //print string  
  3. int str(char *string){  
  4.         printf("\n----PRINT STRING----\n\"%s\"\n",string);  
  5.         return 0;  
  6. }  
  7.   
  8. //interface of this program  
  9. int main(int argc , char **argv){  
  10.         char str_read[1024];  
  11.         printf("Please INPUT something end by [ENTER]\n");  
  12.         scanf("%s",str_read);  
  13.         return str(str_read );  
  14. }  
  15.   
  16.        


  3、產生configure.ac

 

 

    configure.ac是automake的輸入檔案,所以必須先產生該檔案。
    執行命令:

 

[cpp] view plaincopy 
  1. [[email protected] str]# ls  
  2. include  src  
  3. [[email protected] str]# autoscan  
  4. autom4te: configure.ac: no such file or directory  
  5. autoscan: /usr/bin/autom4te failed with exit status: 1  
  6. [[email protected] str]# ls  
  7. autoscan.log  configure.scan  include  src  
  8. [[email protected] str]# cp configure.scan configure.ac   


    修改 configure.ac 

 

 

 

[cpp] view plaincopy 
  1. #                                               -*- Autoconf -*-  
  2. # Process this file with autoconf to produce a configure script.  
  3.   
  4. AC_PREREQ(2.59)  
  5. AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)  
  6. AC_CONFIG_SRCDIR([include/str.h])  
  7. AC_CONFIG_HEADER([config.h])  
  8.   
  9. # Checks for programs.  
  10. AC_PROG_CC  
  11.   
  12. # Checks for libraries.  
  13.   
  14. # Checks for header files.  
  15.   
  16. # Checks for typedefs, structures, and compiler characteristics.  
  17.   
  18. # Checks for library functions.  
  19. AC_OUTPUT  




修改

[cpp] view plaincopy 
  1. AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)  


改為:

 

 

 

[cpp] view plaincopy 
  1. AC_INIT(str,0.0.1, [[email protected]])  


其中:FULL-PACKAGE-NAME 為程式名稱,VERSION為目前的版本, BUG-REPORT-ADDRESS為bug彙報地址

 

 

然後添加兩句話:

    AM_INIT_AUTOMAKE
    AC_CONFIG_FILES([Makefile])

結果如下:(兩句話不是在一起的)

[cpp] view plaincopy 
  1. #                                               -*- Autoconf -*-  
  2. # Process this file with autoconf to produce a configure script.  
  3.   
  4. AC_PREREQ(2.59)  
  5. #AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)  
  6. AC_INIT(str, 0.0.1, [[email protected]])  
  7. AM_INIT_AUTOMAKE  
  8. AC_CONFIG_SRCDIR([include/str.h])  
  9. AC_CONFIG_HEADER([config.h])  
  10.   
  11. # Checks for programs.  
  12. AC_PROG_CC  
  13.   
  14. # Checks for libraries.  
  15.   
  16. # Checks for header files.  
  17.   
  18. # Checks for typedefs, structures, and compiler characteristics.  
  19.   
  20. # Checks for library functions.  
  21. AC_CONFIG_FILES([Makefile])  
  22. AC_OUTPUT  



4、執行aclocal

[cpp] view plaincopy 
  1. [[email protected] str]# aclocal  
  2. /usr/share/aclocal/libfame.m4:6: warning: underquoted definition of AM_PATH_LIBFAME  
  3.   run info ‘(automake)Extending aclocal‘  
  4.   or see http://sources.redhat.com/automake/automake.html#Extending-aclocal  


5、製作Makefile.am

[cpp] view plaincopy 
  1. [[email protected] str]# vi Makefile.am  
  2. #Makefile.am  
  3. bin_PROGRAMS    = str  
  4. str_SOURCES     = include/str.h src/str.c  
  5. str_CPPFLAGS    = -I include/  


automake 這個命令需要用到這個設定檔。各個選項意思比較直觀,不多說。

 

 

 

6、autoheader

 

[cpp] view plaincopy 
  1. [[email protected] str]# autoheader  


7、automake必須檔案:

[cpp] view plaincopy 
  1. *  install-sh  
  2. * missing  
  3. * INSTALL  
  4. * NEWS  
  5. * README  
  6. * AUTHORS  
  7. * ChangeLog  
  8. * COPYING  
  9. * depcomp   


其中,以下檔案在執行automake -a的時候會自動產生

 

 

 

[cpp] view plaincopy 
  1. * install-sh  
  2. * missing  
  3. * INSTALL  
  4. * COPYING  
  5. * depcomp   


所以,接下來手動產生剩下的檔案

 

 

 

[cpp] view plaincopy 
  1. [[email protected] str]# touch NEWS README AUTHORS ChangeLog  


8、執行automake -a

 

 

 

[cpp] view plaincopy 
  1. [[email protected] str]# automake -a  
  2. configure.ac: installing `./install-sh‘  
  3. configure.ac: installing `./missing‘  
  4. Makefile.am: installing `./INSTALL‘  
  5. Makefile.am: installing `./COPYING‘  
  6. Makefile.am: installing `./compile‘  
  7. Makefile.am: installing `./depcomp‘  


9、autoconf

[cpp] view plaincopy 
  1. [[email protected] str]# autoconf  
  2. [[email protected] str]# ls  
  3. aclocal.m4      autoscan.log  config.h.in   configure.scan  include     Makefile.am  NEWS  
  4. AUTHORS         ChangeLog     configure     COPYING         INSTALL     Makefile.in  README  
  5. autom4te.cache  compile       configure.ac  depcomp         install-sh  missing      src  


10、執行測試:
      執行./configure

[cpp] view plaincopy 
  1. [[email protected] str]# ./configure --prefix=/u  
  2. checking for a BSD-compatible install... /usr/bin/install -c  
  3. checking whether build environment is sane... yes  
  4. checking for gawk... gawk  
  5. checking whether make sets $(MAKE)... yes  
  6. checking for gcc... gcc  
  7. checking for C compiler default output file name... a.out  
  8. checking whether the C compiler works... yes  
  9. checking whether we are cross compiling... no  
  10. checking for suffix of executables...  
  11. checking for suffix of object files... o  
  12. checking whether we are using the GNU C compiler... yes  
  13. checking whether gcc accepts -g... yes  
  14. checking for gcc option to accept ANSI C... none needed  
  15. checking for style of include used by make... GNU  
  16. checking dependency style of gcc... gcc3  
  17. configure: creating ./config.status  
  18. config.status: creating Makefile  
  19. config.status: creating config.h  
  20. config.status: config.h is unchanged  
  21. config.status: executing depfiles commands  


執行 make

[cpp] view plaincopy 
  1. [[email protected] str]# make  
  2. make  all-am  
  3. make[1]: Entering directory `/data/devel/c/str‘  
  4. if gcc -DHAVE_CONFIG_H -I. -I. -I.  -I include/   -g -O2 -MT str-str.o -MD -MP -MF ".deps/str-str.Tpo" -c -o str-str.o `test -f ‘src/str.c‘ || echo ‘./‘`src/str.c; \  
  5. then mv -f ".deps/str-str.Tpo" ".deps/str-str.Po"; else rm -f ".deps/str-str.Tpo"; exit 1; fi  
  6. gcc  -g -O2   -o str  str-str.o  
  7. make[1]: Leaving directory `/data/devel/c/str‘  


此時已經產生了 str(可執行檔名字在前面設定Makefile.am的參數時候去頂)這個,可以通過./str直接看到運行結果

[cpp] view plaincopy 
  1. [[email protected]calhost str]# ./str  
  2. Please INPUT something end by [ENTER]  
  3. abcksdhfklsdklfdjlkfd  
  4.   
  5. ----PRINT STRING----  
  6. "abcksdhfklsdklfdjlkfd"  


不過這裡我們都做一步,把它安裝到系統裡面,這樣我們只要在終端輸入str就可以運行程式了。

 

 

 執行 make install:

 

[cpp] view plaincopy 
  1. [[email protected] str]# make install  
  2. make[1]: Entering directory `/data/devel/c/str‘  
  3. test -z "/u/bin" || mkdir -p -- "/u/bin"  
  4.   /usr/bin/install -c ‘str‘ ‘/u/bin/str‘  
  5. make[1]: Nothing to be done for `install-data-am‘.  
  6. make[1]: Leaving directory `/data/devel/c/str‘       


接下來你可以make clean 清除安裝的那些.o 檔案了。

 

 

這樣產生了一個自動的Makefile。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.