自動產生Linux下Makefile全攻略

來源:互聯網
上載者:User

作為Linux下的程式開發人員,大家一定都遇到過Makefile,用make命令來編譯自己寫的程式確實是很方便。一般情況下,大家都是手工寫一個簡單Makefile,如果要想寫出一個符合自由軟體慣例的Makefile就不那麼容易了。

  在本文中,將給大家介紹如何使用 autoconf和automake兩個工具來協助我們自動地產生符合自由軟體慣例的Makefile,這樣就可以象常見的GNU程式一樣,只要使用“./configure”,“make”,“make install”就可以把程式安裝到Linux系統中去了。這將特別適合想做開放原始碼軟體的程式開發人員,又或如果你只是自己寫些小的Toy程式,那麼這個文章對你也會有很大的協助。

編譯一個簡單的源檔案main.c,需要自動產生一個makefile,以下是步驟:

第一步:

----------

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

------------------------------------------------

#include <stdio.h>

int main(int argc, char** argv)

{

    printf("Hello, Auto Makefile!/n");

    return 0;

}

------------------------------------------------

此時狀態如下:

[root@localhost main]# pwd

/root/project/main

[root@localhost main]# ls

main.c

[root@localhost main]#

第二步:

----------

運行 autoscan , 自動建立兩個檔案: autoscan.log configure.scan

此時狀態如下:

[root@localhost main]# autoscan

[root@localhost main]# ls

autoscan.log configure.scan main.c

[root@localhost main]#

第三步:

----------

修改configure.scan的檔案名稱為configure.in

查看configure.in的內容:

------------------------------------------------

#                                               -*- Autoconf -*-

# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.61)

AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)

AC_CONFIG_SRCDIR([main.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:

# 確保使用的是足夠新的Autoconf版本。如果用於建立configure的Autoconf的版

# 本比version 要早,就在標準錯誤輸出列印一條錯誤訊息並不會建立configure。

AC_PREREQ(2.61)

#

# 初始化,定義軟體的基本資料,包括設定包的全稱,版本號碼以及報告BUG時需要用的郵箱地址

#

AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)

#

# 用來偵測所指定的源碼檔案是否存在,來確定源碼目錄的有效性

#

AC_CONFIG_SRCDIR([main.c])

#

# 用於產生config.h檔案,以便autoheader使用

#

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.

#

# 建立輸出檔案。在`configure.in'的末尾調用本宏一次。

#

AC_OUTPUT

------------------------------------------------

修改動作:

    1.修改AC_INIT裡面的參數: AC_INIT(main,1.0, pgpxc@163.com)

    2.添加宏AM_INIT_AUTOMAKE, 它是automake所必備的宏,也同前面一樣,PACKAGE是所要產生軟體套件的名稱,VERSION是版本編號。

    3.在AC_OUTPUT後添加輸出檔案Makefile

修改後的結果:

------------------------------------------------

#                                               -*- Autoconf -*-

# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.61)

AC_INIT(main, 1.0, pgpxc@163.com)

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,該檔案主要處理本地的宏定義。

此時的狀態是:

[root@localhost main]# aclocal

[root@localhost main]# ls

aclocal.m4 autom4te.cache autoscan.log configure.in configure.in~ main.c

[root@localhost main]#

第五步:

運行 autoconf, 目的是產生 configure

此時的狀態是:

[root@localhost main]# autoconf

[root@localhost main]# ls

aclocal.m4      autoscan.log configure.in   main.c

autom4te.cache configure     configure.in~

[root@localhost main]#

第六步:

運行 autoheader,它負責產生config.h.in檔案。該工具通常會從“acconfig.h”檔案中複製使用者附加的符號定義,因此此處沒有附加符號定義,所以不需要建立“acconfig.h”檔案。

此時的狀態是:

[root@localhost main]# autoheader

[root@localhost main]# ls

aclocal.m4      autoscan.log configure     configure.in~

autom4te.cache config.h.in   configure.in main.c

[root@localhost main]#

第七步:

下面即將運行 automake, 但在此之前應該做一下準備工作!

首先

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

這個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等級,它只檢測必須的檔案。

    bin_PROGRAMS定義要產生的執行檔案名稱。如果要產生多個執行檔案,每個檔案名稱用空格隔開。

    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自動添加有一些必需的指令檔。

運行後的狀態是:

------------------------------------------------

[root@localhost main]# automake --add-missing

configure.in:8: installing `./missing'

configure.in:8: installing `./install-sh'

Makefile.am: installing `./depcomp'

[root@localhost 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~

[root@localhost main]#

------------------------------------------------

第八步

運行configure,在這一步中,通過運行自動設定設定檔案configure,把Makefile.in變成了最終的Makefile。

啟動並執行結果如下:

------------------------------------------------

[root@localhost 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

[root@localhost 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~

[root@localhost main]#

------------------------------------------------

第九步

運行 make,對設定檔Makefile進行測試一下

此時的狀態如下:

------------------------------------------------

[root@localhost 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'

[root@localhost 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

[root@localhost main]#

------------------------------------------------

第十步

運行產生的檔案 main:

------------------------------------------------

[root@localhost main]# ./main

Hello, Auto Makefile!

[root@localhost main]#

------------------------------------------------

我用的是ubuntu

以上就是全文了.但有一處要改:用aclocal

全報有一個m4檔案有錯.找到報錯的那一行.把變數加個中括弧就可以了.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.