autotools是個系列工具,主要由autoconf、automake、perl語言環境和m4等組成,所包含的命令有5個:
下圖是autotools使用的流程圖:
autotools的安裝: apt-get install autoconf automake autotools-dev m4
autotools使用簡單一實例如下: 準備程式原始碼 mkdir demo_conf vi hello.cpp 內容如下: #include <iostream> using namespace std; int main(){ cout << "hello\n"; }
使用autoscan命令,掃描工作目錄,產生configure.scan 檔案 $ autoscan $ ls autoscan.log configure.scan hello.cpp
將configure.scan檔案重新命名為configure.ac(或configure.in),並做適當修改。在configure.ac檔案中,#號開始的是行注釋,其他都是m4宏命令;configure.ac裡面的宏的主要作用是偵測系統 $ mv configure.scan configure.ac vi configure.ac 修改後的內容如下: # -*- Autoconf -*- # Process this file with autoconf to produce a configure script. AC_PREREQ([2.68]) AC_INIT(hello, 1.0, zhunian0322@163.com) AC_CONFIG_SRCDIR([hello.cpp]) AC_CONFIG_HEADERS([config.h]) AM_INIT_AUTOMAKE(hello,1.0) # 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.ac檔案說明: AC_PREREQ 聲明本文要求的autoconf版本.如本例中的版本2.68 AC_INIT 用來定義軟體的名稱、版本、作者E-mail等 AC_CONFIG_SRCDIR 用來偵測所制定的源碼檔案是否存在,確定源碼目錄的有效性。此處為目前的目錄下的hello.cpp AC_CONFIG_HEADERS 用於產生config.h檔案,以便autoheader命令使用 AM_INIT_AUTOMAKE 是通過手動添加的,他是automake所必備的宏,用於指定軟體名和軟體版本號碼 AC_PROC_CXX 用來指定編譯器。此處是g++ AC_PROC_CC是gcc AC_CONFIG_FILES 用於產生相應的Makefile檔案 AC_OUTPUT 用來設定configure 所要產生的檔案,如果是Makefile,configure 會把它檢查出來的結果帶入makefile.in檔案產生何時的Makefile。使用automake時,還需要一些其他的參數,這些額外的宏用aclocal工具產生
使用aclocal命令,掃描configure.ac檔案產生aclocal.m4檔案,該檔案主要處理本地的宏定義,它根據已經安裝的宏\使用者定義宏和acinclude.m4檔案中的宏將configure.ac檔案需要的宏集中定義到aclocal.m4中 $ aclocal $ ls aclocal.m4 autom4te.cache autoscan.log configure.ac hello.cpp
使用autoconf命令產生configure檔案.這個命令將configure.ac檔案中的宏展開,產生configure指令碼。 $ autoconf $ ls aclocal.m4 autom4te.cache autoscan.log configure configure.ac hello.cpp
使用autoheader命令產生config.h.in檔案.該命令通常會中acconfig.h 檔案中複製使用者附加的符號定義 $ autoheader $ ls aclocal.m4 autoscan.log configure hello.cpp autom4te.cache config.h.in configure.ac
手工建立Makefile.am 檔案。automake工具會根據configure.in中的參量把Makefile.am轉換成Makefile.in檔案 $ vi Makefile.am 增加一下內容: AUTUMAKE_OPTIONS = foreign bin_PROGRAMS = hello hello_SOURCES = hello.cpp Makefile.am檔案內容說明 AUTOMAKE_OPTIONS 為設定automake 的選項.由於GNU對自己發布的軟體有嚴格的規範,比如必須附帶許可證聲明檔案COPYING等,否則automake執行時會報錯.automake提供了3種軟體等級:foreign、gnu、gnits,供使用者選擇。預設層級是gnu。 bin_PROGRAMS 定義要產生的執行檔案名稱。如果要產生多個執行檔案,每個檔案名稱用空格隔開 hello_SOURCES 定義“hello”這個可以執行程式所需的原始檔案。如果“hello”這個程式是由多個源檔案所產生的,則必須把它所用到的所有源檔案都列出來,並用空格隔開。如果要定義多個可執行程式,那麼需要對每個可執行程式建立對應的file_SOURCES
使用automake 命令產生Makefile.in 檔案.使用選項"--add-missing" 可以讓automake自動添加一些必須的指令檔 $ automake --add-missing configure.ac:8: installing `./install-sh'; error while making link: Operation not supported configure.ac:8: installing `./missing'; error while making link: Operation not supported Makefile.am: installing `./INSTALL'; error while making link: Operation not supported <pre name="code" class="plain">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: installing `./COPYING' using GNU General Public License v3 file; error while making link: Operation not supportedMakefile.am: Consider adding the COPYING file to the version control systemMakefile.am: for your code, to avoid questions about which license your project uses.Makefile.am: installing `./depcomp'; error while making link: Operation not supported說明: configure.ac:8: installing `./install-sh'; error while making link: Operation not supported configure.ac:8: installing `./missing'; error while making link: Operation not supported Makefile.am: installing `./depcomp'; error while making link: Operation not supported ##因為把工程放在FAT32分區上,而它不支援link.需要挪到ext3上 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 ##使用touch 命令建立這4個檔案 $ cp -rvf ../demo_conf/ /home/gino/ $ cd /home/gino/demo_conf/ $ touch NEWS $ touch README $ touch AUTHORS $ touch ChangeLog $ automake --add-missing $ ls aclocal.m4 autoscan.log config.h.in configure hello install-sh Makefile.in README AUTHORS ChangeLog config.log configure.ac hello.cpp Makefile missing stamp-h1 autom4te.cache config.h config.status depcomp hello.o Makefile.am NEWS
使用configure命令,把Makefile.in 變成Makefile $ ./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... no checking for mawk... mawk checking whether make sets $(MAKE)... yes checking for g++... g++ checking whether the C++ compiler works... yes checking for C++ compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether we are using the GNU C++ compiler... yes checking whether g++ accepts -g... yes checking for style of include used by make... GNU checking dependency style of g++... gcc3 configure: creating ./config.status config.status: creating Makefile config.status: creating config.h config.status: config.h is unchanged config.status: executing depfiles commands Makefile的用法 make 命令,用來執行編譯代碼,預設執行"make all"命令. make clean 命令清除編譯時間的obj檔案. make install 命令把目標檔案安裝到系統中. make uninstall 命令把目標檔案從系統中卸載. make dist 命令將程式和相關的文檔打包為一個壓縮文檔,以供發布. make -n 非執行模式,輸出所有執行命令,但不執行. make -c 讀取Makefile之前改變到指定目錄 make -h 顯示所有make選項