Ubuntu 下AutoMake相關流程介紹

來源:互聯網
上載者:User

公司的項目既使用手寫的makefile,也使用automake,沒有統一的規定,一直是把別人的makefile檔案拿過來,改改再用。最近有空,仔細瞭解了一下makefile和automake,發現makefile寫起來麻煩,automake簡單多了,於是將automake的用法整理了一下,作為以後的參考

整個過程的步驟如下:
1. 在存放原始碼的目錄下執行autoscan命令產生configure.scan檔案。
2. 將configure.scan檔案改名為configure.in或者configure.ac,並對其預設配置進行修改,修改如下:

Configure.in檔案:

# Process this file with Autoconf to produce a configure script. 
AC_INIT(Main.cpp)      #指定main函數所在的檔案 
AM_INIT_AUTOMAKE(hello, 1.0)    #指定程式名稱和版本 
# Checks for programs. 
#檢查可用的編譯器 
AC_PROG_CC          #C語言編譯器 
AC_PROG_CPP        #C++編譯器 
AC_PROG_CXX 
# Checks for libraries. 
# Checks for header files. 
# Checks for typedefs, structures, and compiler characteristics. 
# Checks for library functions. 
AC_OUTPUT(Makefile) 

3. 執行aclocal、autoconf兩個命令,分別產生aclocal.m4、configure檔案。

4. 建立一個名為Makefile.am的檔案,並輸入相應的內容。

 

Makefile.am檔案:

AUTOMAKE_OPTIONS=foreign

bin_PROGRAMS=helloworld

helloworld_SOURCES=helloworld.c

5. 執行automake --add-missing,它根據Makefile.am檔案,產生Makefile.in。
6. 執行./configure指令檔,它根據Makefile.in檔案,產生最終的Makefile檔案。
通過以上步驟,在原始碼所在目錄下自動產生了Makefile檔案。

如所示


 

如果覺得上面的步驟太多,也可以使用autoreconf工具,它會按照順序調用上面的工具,步驟如下
1. 在存放原始碼的目錄下執行autoscan命令產生configure.scan檔案。
2. 將configure.scan檔案改名為configure.in,並對其預設配置進行修改。
3. 建立一個名為Makefile.am的檔案,並輸入相應的內容。
4. 執行autoreconf --install,它根據Makefile.am,configure.in檔案,產生Makefile.in,configure,config.h.in。
5. 執行./configure指令檔,它根據Makefile.in檔案,產生最終的Makefile檔案。

如所示


 

上面介紹了產生Makefile的整個流程,其中包括兩個關鍵的檔案,configure.in和Makefile.am,這兩個檔案都需要人工編寫,下面給出一個簡單的樣本,詳細的解釋目前寫不出來,等有一定的經驗之後再寫

Configure.in檔案:

# Process this file with Autoconf to produce a configure script.
AC_INIT(Main.cpp) #指定main函數所在的檔案
AM_INIT_AUTOMAKE(hello, 1.0) #指定程式名稱和版本
# Checks for programs.
#檢查可用的編譯器
AC_PROG_CC #C語言編譯器
AC_PROG_CPP #C++編譯器
AC_PROG_CXX
# Checks for libraries.
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_OUTPUT(Makefile)

Makefile.am檔案:

bin_PROGRAMS = hello # 產生的可執行檔名稱.
hello_SOURCES = Main.cpp A.h A.cpp B.h B.cpp # 程式依賴的源檔案

Linux下Makefile 的automake產生詳解

使用GNU Autoconf/Automake建立Makefile

聯繫我們

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