<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 1,  comments - 0,  trackbacks - 0

    網(wǎng)絡(luò)資源:http://www.worldhello.net/2010/04/07/954.html

    MakeFile文件簡介

    Makefile是用于自動編譯和鏈接的,一個(gè)工程有很多文件組成,每一個(gè)文件的改變都會導(dǎo)致工程的重新鏈接,但是不是所有的文件都需要重新編譯,Makefile中紀(jì)錄有文件的信息,在make時(shí)會決定在鏈接的時(shí)候需要重新編譯哪些文件。

      Makefile的宗旨就是:讓編譯器知道要編譯一個(gè)文件需要依賴其他的哪些文件。當(dāng)那些依賴文件有了改變,編譯器會自動的發(fā)現(xiàn)最終的生成文件已經(jīng)過時(shí),而重新編譯相應(yīng)的模塊。

      Makefile的基本結(jié)構(gòu)不是很復(fù)雜,但當(dāng)一個(gè)程序開發(fā)人員開始寫Makefile時(shí),經(jīng)常會懷疑自己寫的是否符合慣例,而且自己寫的Makefile經(jīng)常和自己的開發(fā)環(huán)境相關(guān)聯(lián),當(dāng)系統(tǒng)環(huán)境變量或路徑發(fā)生了變化后,Makefile可能還要跟著修改。這樣就造成了手工書寫Makefile的諸多問題,automake恰好能很好地幫助我們解決這些問題。

      使用automake,程序開發(fā)人員只需要寫一些簡單的含有預(yù)定義宏的文件,由autoconf根據(jù)一個(gè)宏文件生成configure,由automake根據(jù)另一個(gè)宏文件生成Makefile.in,再使用configure依據(jù)Makefile.in來生成一個(gè)符合慣例的Makefile。下面我們將詳細(xì)介紹Makefile的automake生成方法。

    -使用環(huán)境
    本文所提到的程序是基于Linux發(fā)行版本:Ubuntu10.04,它包含了我們要用到的autoconf,automake。

    -具體步驟
    對于一個(gè)較大的項(xiàng)目而言,完全手動建立Makefile是一件費(fèi)力而又容易出錯(cuò)的工作。autotools系列工具只需用戶輸入簡單的目標(biāo)文件、依賴文件、文件目錄等就可以比較輕松地生成Makefile了。現(xiàn)在Linux上的軟件開發(fā)一般都是用autotools來制作Makefile。

    具體實(shí)例:
    ----------
    在/root/project/main目錄下創(chuàng)建一個(gè)文件main.c,其內(nèi)容如下:
    ------------------------------------------------
    #include <stdio.h>
    int main(int argc, char** argv)
    {
        printf("Hello, Auto Makefile!\n");
        return 0;
    }
    ------------------------------------------------
    具體步驟如下:
     (1)autoscan
             使用autoscan在給定目錄及其子目錄樹中檢查源文件,如果沒有給出目錄,就在當(dāng)前目錄及其子目錄樹中進(jìn)行檢查。最終生成兩個(gè)文件:configure.scan、autoscan.log
    (2)修改configure.scan的文件名為configure.in
              命令:mv configure.scan  configure.in
    (3)修改configure.in的內(nèi)容:
               configure.in的內(nèi)容說明:
                           configure.in文件內(nèi)容如下:
                                   # -*- 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.cpp])
                                    AC_CONFIG_HEADER([config.h])

                                    # Checks for programs.
                                    AC_PROG_CXX

                                    # Checks for libraries.

                                    # Checks for header files.

                                    # Checks for typedefs, structures, and compiler characteristics.

                                    # Checks for library functions.

                                    AC_CONFIG_FILES([Makefile])
                                    AC_OUTPUT        
                                    說明:

                                        1、以“#”號開始的是行為注釋。
                                        2、AC_PREREQ宏聲明本文件要求的autoconf版本。
                                        3、AC_INIT宏用來定義軟件的名稱和版本等信息,這里的BUG-REPORT-                                          ADDRESS可以省略。
                                        4、AC_CONFIG_SRCDIR宏用來偵測所指定的源碼文件是否存在,
                                                來確定源碼目錄的有效性。這個(gè)參數(shù)一般不需要修改。

                                        5、AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader使用。
                                               修改時(shí)需要增加一個(gè)宏AM_INIT_AUTOMAKE(PACKAGE,VERSION)

                                       具體如下:

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

                                          AC_PREREQ(2.61)
                                          AC_INIT(main, 1.0,
    wyylling@126.com)
                                          AM_INIT_AUTOMAKE(main,1.0)
                                          AC_CONFIG_SRCDIR([main.cpp])
                                          #AC_CONFIG_HEADER([config.h])
                                          AM_CONFIG_HEADER([config.h])

                                            # Checks for programs.
                                            AC_PROG_CXX

                                            # Checks for libraries.

                                            # Checks for header files.

                                            # Checks for typedefs, structures, and compiler characteristics.

                                            # Checks for library functions.

                                            AC_CONFIG_FILES([Makefile])
                                            AC_OUTPUT

     我的configure.in下面的真實(shí)內(nèi)容:
                

     

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

    AC_PREREQ([2.65])
    AC_INIT(lda, 1.0, 1527zhaobin@163.com)
    AM_INIT_AUTOMAKE(lda,1.0)
    AC_CONFIG_SRCDIR([lda.h])
    AC_CONFIG_HEADERS([config.h])

    # Checks for programs.
    AC_PROG_CXX
    AC_PROG_CC

    # Checks for libraries.

    # Checks for header files.
    AC_CHECK_HEADERS([stdlib.h string.h])

    # Checks for typedefs, structures, and compiler characteristics.

    # Checks for library functions.
    AC_FUNC_MALLOC
    AC_FUNC_REALLOC
    AC_CHECK_FUNCS([memset])

    AC_CONFIG_FILES([Makefile])
    AC_OUTPUT

    (4)運(yùn)行aclocal,生成一個(gè)“aclocal.m4”文件和一個(gè)緩沖文件夾autom4te.cache,該文件主要處理本地的宏定義。
    此時(shí)的狀態(tài)是:
    [root@localhost main]# aclocal
    [root@localhost main]# ls
    aclocal.m4 autom4te.cache autoscan.log configure.in configure.in~ main.c
    [root@localhost main]#
    (5)運(yùn)行 autoconf, 目的是生成 configure
    此時(shí)的狀態(tài)是:
    [root@localhost main]# autoconf
    [root@localhost main]# ls
    aclocal.m4      autoscan.log configure.in   main.c
    autom4te.cache configure     configure.in~
    [root@localhost main]#
    (6)運(yùn)行 autoheader,它負(fù)責(zé)生成config.h.in文件。該工具通常會從“acconfig.h”文件中復(fù)制用戶附加的符號定義,因此此處沒有附加符號定義,所以不需要?jiǎng)?chuàng)建“acconfig.h”文件。
    此時(shí)的狀態(tài)是:
    [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]#
    (7)配置腳本文件Makefile.am
    新建Makefile.am文件,命令:
    $ vi Makefile.am

    內(nèi)容如下:
    AUTOMAKE_OPTIONS=foreign
    bin_PROGRAMS=helloworld
    helloworld_SOURCES=helloworld.c

    automake會根據(jù)你寫的Makefile.am來自動生成Makefile.in。

    下面對該腳本文件的對應(yīng)項(xiàng)進(jìn)行解釋。

        其中的AUTOMAKE_OPTIONS為設(shè)置automake的選項(xiàng)。由于GNU(在第1章中已經(jīng)有所介紹)對自己發(fā)布的軟件有嚴(yán)格的規(guī)范,比如必須附 帶許可證聲明文件COPYING等,否則automake執(zhí)行時(shí)會報(bào)錯(cuò)。automake提供了三種軟件等級:foreign、gnu和gnits,讓用 戶選擇采用,默認(rèn)等級為gnu。在本例使用foreign等級,它只檢測必須的文件。
            bin_PROGRAMS定義要產(chǎn)生的執(zhí)行文件名。如果要產(chǎn)生多個(gè)執(zhí)行文件,每個(gè)文件名用空格隔開。
            main_SOURCES定義“main”這個(gè)執(zhí)行程序所需要的原始文件。如果”main”這個(gè)程序是由多個(gè)原始文件所產(chǎn)生的,則必須把它所用到的所有原 始文件都列出來,并用空格隔開。例如:若目標(biāo)體“main”需要“main.c”、“sunq.c”、“main.h”三個(gè)依賴文件,則定義 main_SOURCES=main.c sunq.c main.h。要注意的是,如果要定義多個(gè)執(zhí)行文件,則對每個(gè)執(zhí)行程序都要定義相應(yīng)的file_SOURCES。例如,某個(gè)Makefile文件內(nèi)容如下(編譯生成三個(gè)文件:hello,pthread,tty):

    AUTOMAKE_OPTIONS = foreign
    bin_PROGRAMS = hello pthread tty
    hello_SOURCES = hello.c
    pthread_SOURCES = pthread.c
    tty_SOURCES = tty.c

    我的Makefile.am文件內(nèi)容:
    AUTO_OPTIONS=foreign
    bin_PROGRAMS= lda
    lda_SOURCES= lda.cc file_access.cc document.cc lda.h file_access.h document.h
    (8)將運(yùn)行 automake
    [root@localhost main]# automake --add-missing
    使用automake對其生成“configure.in”文件,在這里使用選項(xiàng)“—adding-missing”可以讓automake自動添加有一些必需的腳本文件。
    運(yùn)行后的狀態(tài)是:
    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]#
    在這一步中往往會產(chǎn)生一種錯(cuò)誤如下:

    configure.in: required file `./install-sh' not found
    configure.in: required file `./mkinstalldirs' not found
    configure.in: required file `./missing' not found
    Makefile.am: required file `./COPYING' not found
    Makefile.am: required file `./INSTALL' not found
    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 `./depcomp' not found

    我們將進(jìn)行如下處理:

    Additionally, the INSTALL, NEWS, README, COPYING, AUTHORS and ./ChangeLog files need to be created:

     

    touch INSTALL NEWS README AUTHORS ChangeLog

    (9)運(yùn)行configure,在這一步中,通過運(yùn)行自動配置設(shè)置文件configure,把Makefile.in變成了最終的Makefile。
    運(yùn)行的結(jié)果如下:
    ------------------------------------------------
    [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]#
    (10)運(yùn)行 make,對配置文件Makefile進(jìn)行測試一下

    此時(shí)的狀態(tài)如下:
    ------------------------------------------------
    [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]#
    ------------------------------------------------
    (11)運(yùn)行生成的文件 main:
    ------------------------------------------------
    [root@localhost main]# ./main
    Hello, Auto Makefile!
    [root@localhost main]#
    補(bǔ)充:
     Makefile

    在符合GNU Makefiel慣例的Makefile中,包含了一些基本的預(yù)先定義的操作:

    make

    根據(jù)Makefile編譯源代碼,連接,生成目標(biāo)文件,可執(zhí)行文件。

    make clean

    清除上次的make命令所產(chǎn)生的object文件(后綴為“.o”的文件)及可執(zhí)行文件。

    make install

    將編譯成功的可執(zhí)行文件安裝到系統(tǒng)目錄中,一般為/usr/local/bin目錄。

    make dist

    產(chǎn)生發(fā)布軟件包文件(即distribution package)。這個(gè)命令將會將可執(zhí)行文件及相關(guān)文件打包成一個(gè)tar.gz壓縮的文件用來作為發(fā)布軟件的軟件包。

    它會在當(dāng)前目錄下生成一個(gè)名字類似“PACKAGE-VERSION.tar.gz”的文件。PACKAGE和VERSION,是我們在configure.in中定義的AM_INIT_AUTOMAKE(PACKAGE, VERSION)。

    make distcheck

    生成發(fā)布軟件包并對其進(jìn)行測試檢查,以確定發(fā)布包的正確性。這個(gè)操作將自動把壓縮包文件解開,然后執(zhí)行configure命令,并且執(zhí)行make,來確認(rèn)編譯不出現(xiàn)錯(cuò)誤,最后提示你軟件包已經(jīng)準(zhǔn)備好,可以發(fā)布了。

    ===============================================
    helloworld-1.0.tar.gz is ready for distribution
    ===============================================
    make distclean

    類似make clean,但同時(shí)也將configure生成的文件全部刪除掉,包括Makefile。

























     

    posted on 2012-04-24 20:33 憤怒的考拉 閱讀(694) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿

    隨筆檔案

    文章檔案

    搜索

    •  

    最新評論

    主站蜘蛛池模板: 337p日本欧洲亚洲大胆艺术| 日韩中文字幕精品免费一区| 好男人视频社区精品免费| 亚洲人成色在线观看| 久久激情亚洲精品无码?V| **实干一级毛片aa免费| 色偷偷亚洲男人天堂| 97人伦色伦成人免费视频| 成人a毛片免费视频观看| 久久亚洲AV成人出白浆无码国产| 在线观看无码的免费网站| 丁香花在线视频观看免费| 亚洲综合无码AV一区二区| 一个人免费观看视频在线中文| 波多野结衣免费视频观看| 国产精品免费福利久久| 亚洲天堂一区二区| 免费精品国产自产拍观看| 亚洲AV无码一区二区一二区| 在线精品免费视频| 性xxxx视频免费播放直播| 精品在线免费视频| 亚洲国产精品人久久电影| 亚洲欧洲成人精品香蕉网| 久久久久久一品道精品免费看| 亚洲精品久久无码av片俺去也| 免费高清在线影片一区| 免费国产成人午夜在线观看| 亚洲黄色三级视频| 中文亚洲成a人片在线观看| 免费看片A级毛片免费看| 1000部免费啪啪十八未年禁止观看 | 亚洲精品国产精品乱码不卡√| 午夜精品在线免费观看| 99久久精品国产亚洲| 亚洲综合av永久无码精品一区二区 | 无码国产精品一区二区免费式芒果| 精品亚洲成A人在线观看青青| 亚洲国产午夜电影在线入口| 久久久亚洲精品视频| 亚洲男人天堂2020|