新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 自動生成 Makefile 的全過程詳解

        自動生成 Makefile 的全過程詳解

        作者: 時間:2016-10-10 來源:網絡 收藏

        4 、新建Makefile.am

        新建Makefile.am 文件,命令:

        $ vi Makefile.am

        內容如下:

        AUTOMAKE_OPTIONS=foreign

        bin_PROGRAMS=helloworld

        helloworld_SOURCES=helloworld.c

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

        Makefile.am 中定義的宏和目標, 會指導automake 生成指定的代碼。例如,宏bin_PROGRAMS 將導致編譯和連接的目標被生成。

        5 、運行automake

        命令:

        $ automake --add-missing

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

        configure.in: installing `./mkinstalldirs'

        configure.in: installing `./missing'

        Makefile.am: installing `./depcomp'

        automake 會根據Makefile.am 文件產生一些文件,包含最重要的Makefile.in 。

        6 、執行configure 生成Makefile

        $ ./configure

        checking for a BSD-compatible install... /usr/bin/install -c

        checking whether build environment is sane... yes

        checking for gawk... gawk

        checking whether make sets $(MAKE)... yes

        checking for gcc... gcc

        checking for C compiler default output... 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 ANSI C... 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: executing depfiles commands

        $ ls -l Makefile

        -rw-rw-r-- 1 yutao yutao 15035 Oct 15 10:40 Makefile

        你可以看到,此時Makefile 已經產生出來了。

        7 、使用Makefile 編譯代碼

        $ make

        if gcc -DPACKAGE_NAME= -DPACKAGE_TARNAME= -DPACKAGE_VERSION= -

        DPACKAGE_STRING= -DPACKAGE_BUGREPORT= -DPACKAGE=helloworld -DVERSION=1.0

        -I. -I. -g -O2 -MT helloworld.o -MD -MP -MF .deps/helloworld.Tpo

        -c -o helloworld.o `test -f 'helloworld.c' || echo './'`helloworld.c;

        then mv -f .deps/helloworld.Tpo .deps/helloworld.Po;

        else rm -f .deps/helloworld.Tpo; exit 1;

        fi

        gcc -g -O2 -o helloworld helloworld.o

        運行helloworld

        $ ./helloworld

        Hello, Linux World!

        這樣helloworld 就編譯出來了,你如果按上面的步驟來做的話,應該也會很容易地編譯出正確的helloworld 文件。你還可以試著使用一些其 他的make 命令,如make clean ,make install ,make dist ,看看它們會給你什么樣的效果。感覺如何?自己也能寫出這么專業的Makefile ,老板一定會對你刮目相看。

        四、深入淺出

        針對上面提到的各個命令,我們再做些詳細的介紹。

        1 、 autoscan

        autoscan 是用來掃描源代碼目錄生成configure.scan 文件的。autoscan 可以用目錄名做為參數,但如果你不使用參數的話,那么 autoscan 將認為使用的是當前目錄。autoscan 將掃描你所指定目錄中的源文件,并創建configure.scan 文件。

        2 、 configure.scan

        configure.scan 包含了系統配置的基本選項,里面都是一些宏定義。我們需要將它改名為configure.in

        3 、 aclocal

        aclocal 是一個perl 腳本程序。aclocal 根據configure.in 文件的內容,自動生成aclocal.m4 文件。aclocal 的定義是:“aclocal - create aclocal.m4 by scanning configure.ac” 。

        4 、 autoconf

        autoconf 是用來產生configure 文件的。configure 是一個腳本,它能設置源程序來適應各種不同的操作系統平臺,并且根據不同的系統來產生合適的Makefile ,從而可以使你的源代碼能在不同的操作系統平臺上被編譯出來。

        configure.in 文件的內容是一些宏,這些宏經過autoconf 處理后會變成檢查系統特性、環境變量、軟件必須的參數的shell 腳本。configure.in 文件中的宏的順序并沒有規定,但是你必須在所有宏的最前 面和最后面分別加上AC_INIT 宏和AC_OUTPUT 宏。

        在configure.ini 中:

        # 號表示注釋,這個宏后面的內容將被忽略。

        AC_INIT(FILE)

        這個宏用來檢查源代碼所在的路徑。

        AM_INIT_AUTOMAKE(PACKAGE, VERSION)

        這個宏是必須的,它描述了我們將要生成的軟件包的名字及其版本號:PACKAGE 是軟件包的名字,VERSION 是版本號。當你使用make dist 命令時,它會給你生成一個類似helloworld-1.0.tar.gz 的軟件發行包,其中就有對應的軟件包的名字和版本號。

        AC_PROG_CC

        這個宏將檢查系統所用的C 編譯器。

        AC_OUTPUT(FILE)

        這個宏是我們要輸出的Makefile 的名字。

        我們在使用automake 時,實際上還需要用到其他的一些宏,但我們可以用aclocal 來幫我們自動產生。執行aclocal后我們會得到aclocal.m4 文件。

        產生了configure.in 和aclocal.m4 兩個宏文件后,我們就可以使用autoconf 來產生configure 文件了。

        5 、 Makefile.am

        Makefile.am 是用來生成Makefile.in 的,需要你手工書寫。Makefile.am 中定義了一些內容:

        AUTOMAKE_OPTIONS

        這個是automake 的選項。在執行automake 時,它會檢查目錄下是否存在標準GNU 軟件包中應具備的各種文件,例如AUTHORS 、ChangeLog 、NEWS 等文件。我們將其設置成foreign 時,automake 會改用一般軟件包的標準來檢查。



        關鍵詞: linux

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 贡嘎县| 通化县| 科技| 青川县| 万山特区| 汶川县| 通河县| 金沙县| 正阳县| 望都县| 南丰县| 和田市| 兴安盟| 青阳县| 仁化县| 高要市| 西昌市| 陇川县| 南岸区| 新丰县| 庐江县| 樟树市| 伊金霍洛旗| 肃宁县| 米泉市| 新丰县| 吉林市| 蒲江县| 赣榆县| 通海县| 宁波市| 夏河县| 界首市| 水富县| 廊坊市| 蓝山县| 泸定县| 张掖市| 英吉沙县| 和林格尔县| 惠来县|