automake自動生成makefile檔案
Linux下程式設計時,為了方便編譯,往往使用Makefile檔案自動完成編譯,但是Makefile檔案本身的書寫十分複雜,規則很多。好在Linux為我們提供了自動生成功能完善的Makefile檔案的工具autoconf/automake。本文講述如何使用它們生成Makefile檔案。
整個工具組的工作流程如下圖:
Linux Makefile第一步
在/root/project/main目錄下建立一個檔案main.c,其內容如下:
- #include <stdio.h>
- int main(int argc, char** argv)
- {
- printf("Hello world!\n");
- return 0;
- }
此時狀態如下:
- [[email protected] main]# ls
- main.c
Linux Makefile第二步:
- 執行 autoscan , 自動建立兩個檔案:
- autoscan.log configure.scan此時狀態如下:
- [[email protected] main]# autoscan
- [[email protected] main]# ls
- 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
修改後的結果:
- # -*- Autoconf -*-
- # Process this file with autoconf to produce a configure script.
- AC_PREREQ(2.61)
- AC_INIT(main, 1.0, [email protected])
- AC_CONFIG_SRCDIR([main.c])
- AC_CONFIG_HEADER([config.h])
- AM_INIT_AUTOMAKE(main,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])
第四步:執行 aclocal,
生成一個“aclocal.m4”檔案和一個緩衝資料夾autom4te.cache,該檔案主要處理本地的巨集定義。此時的狀態是:
- [[email protected] main]# aclocal
- [[email protected] main]# ls
- aclocal.m4 autom4te.cache autoscan.log configure.in
- configure.in~ main.c
第五步:執行 autoconf, 目的是生成 configure 此時的狀態是:
- [[email protected] main]# autoconf
- [[email protected] main]# ls
- aclocal.m4 autoscan.log configure.in main.c
- autom4te.cache configure configure.in~
第六步:執行 autoheader,它負責生成config.h.in檔案。
該工具通常會從“acconfig.h”檔案中複製使用者附加的符號定義,因此此處沒有附加符號定義,所以不需要建立“acconfig.h”檔案。此時的狀態是:
- [[email protected] main]# autoheader
- [[email protected] main]# ls
- aclocal.m4 autoscan.log configure configure.in~
- 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的內容如下:
- AUTOMAKE_OPTIONS=foreign
- bin_PROGRAMS=main
- 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自動新增有一些必需的指令碼檔案。
執行後的狀態是:
- [[email protected] main]# automake --add-missing
- configure.in:8: installing `./missing'
- configure.in:8: installing `./install-sh'
- Makefile.am: installing `./depcomp'
- [[email protected] main]# ls
- aclocal.m4 config.h.in configure.in~ main.c Makefile.in
- autom4te.cache configure depcomp Makefile.am missing
- 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。執行的結果如下:
- [[email protected] main]# ./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 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
- [[email protected] main]# ls
- aclocal.m4 config.h.in configure.in main.c Makefile.in
- autom4te.cache config.log configure.in~ Makefile missing
- autoscan.log config.status depcomp Makefile.am stamp-h1
- config.h configure install-sh Makefile.am~
Linux Makefile第九步執行 make,
對配置檔案Makefile進行測試一下此時的狀態如下:
- [[email protected] main]# make
- cd . && /bin/sh /root/project/main/missing --run aclocal-1.10
- cd . && /bin/sh /root/project/main/missing --run automake-1.10 --foreign
- cd . && /bin/sh /root/project/main/missing --run autoconf
- /bin/sh ./config.status --recheck
- running CONFIG_SHELL=/bin/sh /bin/sh ./configure --no-create --no-recursion
- 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 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
- /bin/sh ./config.status
- config.status: creating Makefile
- config.status: creating config.h
- config.status: config.h is unchanged
- config.status: executing depfiles commands
- cd . && /bin/sh /root/project/main/missing --run autoheader
- rm -f stamp-h1
- touch config.h.in
- make all-am
- make[1]: Entering directory `/root/project/main'
- gcc -DHAVE_CONFIG_H -I. -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
- mv -f .deps/main.Tpo .deps/main.Po
- gcc -g -O2 -o main main.o
- cd . && /bin/sh ./config.status config.h
- config.status: creating config.h
- config.status: config.h is unchanged
- make[1]: Leaving directory `/root/project/main'
- [[email protected] main]# ls
- aclocal.m4 autoscan.log config.h.in config.status configure.in depcomp main main.o Makefile.am Makefile.in stamp-h1
- autom4te.cache config.h config.log configure configure.in~ install-sh main.c Makefile Makefile.am~ missing
Linux Makefile第十步執行生成的檔案 main:
- [[email protected] main]# ./main
- Hello world!
- [[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