1. 程式人生 > >automake自動生成makefile檔案

automake自動生成makefile檔案

 Linux下程式設計時,為了方便編譯,往往使用Makefile檔案自動完成編譯,但是Makefile檔案本身的書寫十分複雜,規則很多。好在Linux為我們提供了自動生成功能完善的Makefile檔案的工具autoconf/automake。本文講述如何使用它們生成Makefile檔案。

  整個工具組的工作流程如下圖:


Linux Makefile第一步

在/root/project/main目錄下建立一個檔案main.c,其內容如下:

  1. #include <stdio.h>
  2. int main(int argc, char** argv)   
  3. {   
  4. printf("Hello world!\n");   
  5. return 0;   
  6. }  

此時狀態如下:

  1. [[email protected] main]# ls  
  2. main.c  

Linux Makefile第二步:

  1. 執行 autoscan , 自動建立兩個檔案:   
  2. autoscan.log  configure.scan此時狀態如下:  
  3. [[email protected] main]# autoscan  
  4. [[email protected] main]# ls  
  5. autoscan.log  configure.scan  main.c  

第三步:修改configure.scan的檔名為configure.in檢視configure.in的內容:

修改動作:

1.修改AC_INIT裡面的引數: AC_INIT(main,1.0, [email protected])

2.新增巨集AM_INIT_AUTOMAKE, 它是automake所必備的巨集,也同前面一樣,PACKAGE是所要產生軟體套件的名稱,VERSION是版本編號。
   3.在AC_OUTPUT後新增輸出檔案Linux Makefile

修改後的結果:

  1. #                                               -*- Autoconf -*-  
  2. # Process this file with autoconf to produce a configure script.  
  3. AC_PREREQ(2.61)  
  4. AC_INIT(main, 1.0, [email protected])  
  5. AC_CONFIG_SRCDIR([main.c])  
  6. AC_CONFIG_HEADER([config.h])  
  7. AM_INIT_AUTOMAKE(main,1.0)  
  8. # Checks for programs.  
  9. AC_PROG_CC  
  10. # Checks for libraries.  
  11. # Checks for header files.  
  12. # Checks for typedefs, structures, and compiler characteristics.  
  13. # Checks for library functions.  
  14. AC_OUTPUT([Makefile]) 


第四步:執行 aclocal,

生成一個“aclocal.m4”檔案和一個緩衝資料夾autom4te.cache,該檔案主要處理本地的巨集定義。此時的狀態是:

  1. [[email protected] main]# aclocal  
  2. [[email protected] main]# ls  
  3. aclocal.m4  autom4te.cache  autoscan.log  configure.in  
  4. configure.in~  main.c  

第五步:執行 autoconf, 目的是生成 configure 此時的狀態是:

  1. [[email protected] main]# autoconf  
  2. [[email protected] main]# ls  
  3. aclocal.m4      autoscan.log  configure.in   main.c  
  4. autom4te.cache  configure     configure.in~  

第六步:執行 autoheader,它負責生成config.h.in檔案。

該工具通常會從“acconfig.h”檔案中複製使用者附加的符號定義,因此此處沒有附加符號定義,所以不需要建立“acconfig.h”檔案。此時的狀態是:

  1. [[email protected] main]# autoheader  
  2. [[email protected] main]# ls  
  3. aclocal.m4      autoscan.log  configure     configure.in~  
  4. autom4te.cache  config.h.in   configure.in  main.c  

第七步:下面即將執行 automake, 但在此之前應該做一下準備工作!

首先建立一個 Linux Makefile.am.這一步是建立Linux Makefile很重要的一步,automake要用的指令碼配置檔案是Linux Makefile.am,使用者需要自己建立相應的檔案。之後,automake工具轉換成Linux Makefile.in。

這個Linux Makefile.am的內容如下:

  1. AUTOMAKE_OPTIONS=foreign
  2. bin_PROGRAMS=main
  3. main_SOURCES=main.c 

下面對該指令碼檔案的對應項進行解釋。其中的AUTOMAKE_OPTIONS為設定automake的選項。由於GNU(在第1章中已經有所介紹)對自己釋出的軟體有嚴格的規範,比如必須附帶許可證宣告檔案COPYING等,否則automake執行時會報錯。

automake提供了三種軟體等級:foreign、gnu和gnits,讓使用者選擇採用,預設等級為gnu。在本例使用foreign等級,它只檢測必須的檔案。定義要產生的執行檔名。如果要產生多個執行檔案,每個檔名用空格隔開。

main_SOURCES定義“main”這個執行程式所需要的原始檔案。如果”main”這個程式是由多個原始檔案所產生的,則必須把它所用到的所有原始檔案都列出來,並用空格隔開。例如:若目標體“main”需要“main.c”、“sunq.c”、“main.h”三個依賴檔案,則定義main_SOURCES=main.c sunq.c main.h。要注意的是,如果要定義多個執行檔案,則對每個執行程式都要定義相應的file_SOURCES。

其次使用automake對其生成“configure.in”檔案,在這裡使用選項“—adding-missing”可以讓automake自動新增有一些必需的指令碼檔案。
執行後的狀態是:

  1. [[email protected] main]# automake --add-missing  
  2. configure.in:8: installing `./missing'  
  3. configure.in:8: installing `./install-sh'  
  4. Makefile.am: installing `./depcomp'  
  5. [[email protected] main]# ls  
  6. aclocal.m4      config.h.in   configure.in~  main.c        Makefile.in  
  7. autom4te.cache  configure     depcomp        Makefile.am   missing  
  8. autoscan.log    configure.in  install-sh     Makefile.am~  

此步驟如果報以下錯誤:

 Makefile.am: error: required file './NEWS' not found
    Makefile.am: error: required file './README' not found
    Makefile.am: error: required file './AUTHORS' not found
    Makefile.am: error: required file './ChangeLog' not found

只要touch出以上四個檔案,再重新執行即可。

Linux Makefile第八步執行configure,

在這一步中,通過執行自動配置設定檔案configure,把Makefile.in變成了最終的Makefile。執行的結果如下:

  1. [[email protected] main]# ./configure   
  2. checking for a BSD-compatible install... /usr/bin/install -c  
  3. checking whether build environment is sane... yes  
  4. checking for a thread-safe mkdir -p... /bin/mkdir -p  
  5. checking for gawk... gawk  
  6. checking whether make sets $(MAKE)... yes  
  7. checking for gcc... gcc  
  8. checking for C compiler default output file name... a.out  
  9. checking whether the C compiler works... yes  
  10. checking whether we are cross compiling... no  
  11. checking for suffix of executables...   
  12. checking for suffix of object files... o  
  13. checking whether we are using the GNU C compiler... yes  
  14. checking whether gcc accepts -g... yes  
  15. checking for gcc option to accept ISO C89... none needed  
  16. checking for style of include used by make... GNU  
  17. checking dependency style of gcc... gcc3  
  18. configure: creating ./config.status  
  19. config.status: creating Makefile  
  20. config.status: creating config.h  
  21. config.status: executing depfiles commands  
  22. [[email protected] main]# ls  
  23. aclocal.m4      config.h.in    configure.in   main.c        Makefile.in  
  24. autom4te.cache  config.log     configure.in~  Makefile      missing  
  25. autoscan.log    config.status  depcomp        Makefile.am   stamp-h1  
  26. config.h        configure      install-sh     Makefile.am~  

Linux Makefile第九步執行 make,

對配置檔案Makefile進行測試一下此時的狀態如下:

  1. [[email protected] main]# make  
  2. cd . && /bin/sh /root/project/main/missing --run aclocal-1.10   
  3. cd . && /bin/sh /root/project/main/missing --run automake-1.10 --foreign   
  4. cd . && /bin/sh /root/project/main/missing --run autoconf  
  5. /bin/sh ./config.status --recheck  
  6. running CONFIG_SHELL=/bin/sh /bin/sh ./configure   --no-create --no-recursion  
  7. checking for a BSD-compatible install... /usr/bin/install -c  
  8. checking whether build environment is sane... yes  
  9. checking for a thread-safe mkdir -p... /bin/mkdir -p  
  10. checking for gawk... gawk  
  11. checking whether make sets $(MAKE)... yes  
  12. checking for gcc... gcc  
  13. checking for C compiler default output file name... a.out  
  14. checking whether the C compiler works... yes  
  15. checking whether we are cross compiling... no  
  16. checking for suffix of executables...   
  17. checking for suffix of object files... o  
  18. checking whether we are using the GNU C compiler... yes  
  19. checking whether gcc accepts -g... yes  
  20. checking for gcc option to accept ISO C89... none needed  
  21. checking for style of include used by make... GNU  
  22. checking dependency style of gcc... gcc3  
  23. configure: creating ./config.status  
  24. /bin/sh ./config.status  
  25. config.status: creating Makefile  
  26. config.status: creating config.h  
  27. config.status: config.h is unchanged  
  28. config.status: executing depfiles commands  
  29. cd . && /bin/sh /root/project/main/missing --run autoheader  
  30. rm -f stamp-h1  
  31. touch config.h.in  
  32. make  all-am  
  33. make[1]: Entering directory `/root/project/main'  
  34. gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c  
  35. mv -f .deps/main.Tpo .deps/main.Po  
  36. gcc  -g -O2   -o main main.o    
  37. cd . && /bin/sh ./config.status config.h  
  38. config.status: creating config.h  
  39. config.status: config.h is unchanged  
  40. make[1]: Leaving directory `/root/project/main'  
  41. [[email protected] main]# ls  
  42. aclocal.m4      autoscan.log  config.h.in  config.status  configure.in   depcomp     main    main.o    Makefile.am   Makefile.in  stamp-h1  
  43. autom4te.cache  config.h      config.log   configure      configure.in~  install-sh  main.c  Makefile  Makefile.am~  missing  

Linux Makefile第十步執行生成的檔案 main:

  1. [[email protected] main]# ./main  
  2. Hello world!  
  3. [[email protected] main]#  

 備註:

 如果要執行release版本,則執行make CFLAGS=" -O3" 或者 make CXXFLAGS=" -O3",具體是CFLAGS還是CXXFLAGS,可在Makefile檔案中查詢確定。

本文參考:

http://www.cnblogs.com/njucslzh/archive/2010/04/29/1723320.html

http://os.51cto.com/art/201003/185539.htm

相關推薦

automake自動生成makefile檔案

 Linux下程式設計時,為了方便編譯,往往使用Makefile檔案自動完成編譯,但是Makefile檔案本身的書寫十分複雜,規則很多。好在Linux為我們提供了自動生成功能完善的Makefile檔案的工具autoconf/automake。本文講述如何使用它們生成Make

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

rect -o 創建 otool 其中 ner markdown ted head 一個簡單的執行程序的GNU automake自動生成Makefile的方法及案例 在GNU的世界裏,存在Automake這樣的工具進行自動生成Makefile文件,automake是由Per

qmake使用方法(自動生成Makefile檔案

下面是qmake的簡單介紹和使用要領,更為詳細的資訊請參閱手冊qmake的介紹 手寫Makefile是比較困難並且容易出錯的,尤其是需要給不同的平臺和編譯器組合寫幾個Makefile。使用qmake,開發者建立一個簡單的“專案”檔案並且執行qmake生成適當的Makefile。qmake會注意所有的編譯器和平

一個簡單的執行程式的GNU 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

例解 autoconf 和 automake 生成 Makefile 檔案

引子 無論是在Linux還是在Unix環境中,make都是一個非常重要的編譯命令。不管是自己進行專案開發還是安裝應用軟體,我們都經常要用到make或 make install。利用make工具,我們可以將大型的開發專案分解成為多個更易於管理的模組,對於一個包括幾百個原始檔的

Makefile自動生成依賴檔案,並自動編譯

因為經常要用到makefile編譯,每次都為標頭檔案的依賴關係頭痛,所以這次費了兩天時間開發了一個通用的makefile,它可以自動生成依賴檔案,並編譯,當然你要按檔案裡面的說明來填充一些中間檔名. 程式碼如下: #自動依賴項(*.d),並編譯生成檔案 #編譯選項 LDF

【探索wireshark】 使用autoconf, automake自動生成Makefile

     無論是在Linux還是在Unix環境中,make都是一個非常重要的編譯命令。不管是自己進行專案開發還是安裝應用軟體,我們都經常要用到make或 make install。利用make工具,我們可以將大型的開發專案分解成為多個更易於管理的模組,對於一個包括幾百個

makefile 自動生成依賴檔案

gcc  -c -o a.o a.c -Wp, -MD a.d a.d就是生成的依賴檔案 在a.d中 有 a.o:a.c a.h  (以及其他依賴的標頭檔案等) 在makefile中,包含依賴檔案,使用依賴檔案,使用如下: include a.d %.o : %.c$(CC

教你如何使用automake生成Makefile檔案

作者:鄒祁峰 郵箱:[email protected] 日期:2014.02.21 轉載請註明來自"祁峰"的CSDN部落格 1 引言   眾所周知,Makefile主要用來組織原始碼的編

AutoConf自動生成Makefile(基於helloworld簡單例子)

programs tom change col -a 二進制 自己 int 生成 新建一個簡單的helloworld工程文件夾,目錄結構如下 hello.h代碼: #include<stdio.h> void fprint() { printf("h

自動生成Makefile的全過程詳解

change make auto osc .cn 轉換成 otto 創建 準備 一、簡介 Linux下的程序開發人員,一定都遇到過Makefile,用make命令來編譯自己寫的程序確實是很方便。一般情況下,大家都是手工寫一個簡單Makefile,如果要想寫出一個符合自由軟件

shell自動生成檔案有一個問號的字尾

寫了一個指令碼,自動處理一個檔案。   rm -f session.log rm -f link wget ftp://hostname/f:/ddn/session.log egrep '^N[[:digit:]]|^D[1-4]' session.log >>lin

idea配置自動生成java檔案頭資訊

1.   操作流程:  Settings-> Editor -> File and Code Templates -> Class   2.  配置模板 2. 1 去掉 #parse("File Header.java")

webpack4 系列教程(十三):自動生成HTML檔案

作者按:因為教程所示圖片使用的是 github 倉庫圖片,網速過慢的朋友請移步《webpack4 系列教程(十三):自動生成 HTML 檔案》原文地址。更歡迎來我的小站看更多原創內容:godbmw.com,進行“姿勢”交流 ♪(^∇^*) 0. 課程介紹和資料 >>>本節

windows程式崩潰時自動生成dump檔案方法

  /****************第一步新增createdump.h********************************* 新增一個頭檔案:createdump.h #pragma once #include <windows.h> #inclu

Mybatis 自動生成mapper檔案

  在pom.xml下的<build>內加入: <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId&g

unity 檔案更改自動生成.bytes檔案 與 點選play自動讀取指定路徑excel檔案生成.bytes檔案

using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using Sys

eclipse不自動生成R檔案和R檔案報錯問題

 android 的R.java檔案報錯是個很頭疼的問題,雖然很少遇到,但由於R.java檔案為自動生成的,解決問題一般不容易。 首先,一般此類問題都是由於資原始檔出問題導致的,即使資原始檔並沒有報錯。 遇到此類問題,首先可以嘗試以下兩種方法: 1、右擊你的Android專案——>Android

idea中建立.gitignore檔案忽略提交系統自動生成檔案

網上很多帖子都是新增外掛的方法,此處就不必多說了,可以實現過濾檔案,此處不需要直接下載外掛,直接將檔案拷貝到專案跟路徑下即可, 新增檔案".gitignore"到專案的跟目錄下 其中,.giti

使用AutoMake輕鬆生成Makefile(轉)

% ./configure creating cache ./config.cache checking for a BSD compatible install... /usr/bin/install -c checking whether build environment i