autotools使用


autotools制作makefile

 

下面以hello.c來說明生成makefile的過程。

基本步驟如下:

1)autoscan命令生成configure.scan文件,重命名configure.scan,並修改

2)aclocal命令生成aclocal.m4

3)autoconf命令生成configure

4)新建文件Makefile.am,執行automake命令生成Makefile.in文件

5)運行configure命令生成Makefile

 

具體步驟如下:

1. 先用which命令確認系統是否安裝了以下工具

aclocal

autoscan

autoconf

autoheader

automake

 

2. 新建目錄hello,cd hello,  編寫hello.c

3. 執行命令 autoscan, 會生成configuer.scan和autoscan.log

autoscan.log  configure.scan  hello.c

4. 將configure.scan重命名為configure.in,並編輯。

# -*- Autoconf -*- # Process this file with autoconf to produce a configure script. #AC_PREREQ([2.69]) AC_INIT(hello, 1.0, xxxx@xxxx.cn) AM_INIT_AUTOMAKE(hello, 1.0, xxxx@xxxx.cn) -----------這里是AM,不是AC #AC_CONFIG_SRCDIR([hello.c]) #AC_CONFIG_HEADERS([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(Makefile)





說明:
AC_PREREQ 宏聲明本文件要求的 autoconf 版本
AC_INIT(FULL-PACKAGE-NAME,VERSION,BUG-REPORT-ADDRESS) ,用來定義軟件的名稱和版本等信息 ,第三個參數一般為作者的Email
AM_INIT_AUTOMAKE,是automake必備的宏,使automake自動生成makefile.in

AC_CONFIG_SRCDIR, 用來檢查所指定的源碼文件是否存在,以及確定源碼目錄的有效性。在此處源碼文件為當前目錄下的hello,c
AC_CONFIG_HEADER,用於生產config.h文件,以便autoheader使用
AC_CONFIG_FILES, 用於生成對應的Makefile文件。
 

 

5. 執行命令aclocal,生成aclocal.m4和autom4te.cache文件。

aclocal: warning: autoconf input should be named 'configure.ac', not 'configure.in'  ----不用理會

-> ls

aclocal.m4  autom4te.cache  autoscan.log  configure.in  hello.c

 

 

 

6. 執行命令autocon,生成configure文件

-> ls aclocal.m4 autom4te.cache autoscan.log configure configure.in  hello.c

 

 

7. 新建Makefile.am文件

AUTOMAKE_OPTIONS=foreign bin_PROGRAMS=hello hello_SOURCES=hello.c


其中:
AUTOMAKE_OPTIONS, 為設置automake的選項。GNU 對自己發布的軟件有嚴格的規范,
比如必須附帶許可證聲明文件 COPYING 等,否則 automake 執行時會報錯。 automake 提供了 3
種軟件等級: foreigngnu gnits,讓用戶選擇采用,默認等級為 gnu。在本示例中采用 foreign
等級,它只檢測必須的文件。
bin_PROGRAMS,定義要產生的執行文件名。如果要產生多個執行文件,每個文件名用空格隔開
hello_SOURCES,定義hello可執行文件所需要的原始文件。如果“hello”這個程序是由多個
原始文件所產生的,則必須把它所用到的所有原始文件都列出來,並用空格隔開。例如:若目標
體“hello”需要“hello.c”、“david.c”、“hello.h”三個依賴文件,則定義 hello_SOURCES=hello.c david.c
hello.h。要注意的是 ,如果要定義多個執行文件 ,則對每個執行程序都要定義相應的
file_SOURCES

 

8. 執行命令automake --add-missing,生成Makefile.in文件

automake -a或者automake --add-missing,可以讓automake自動添加一些必須的腳本文件。

automake: warning: autoconf input should be named 'configure.ac', not 'configure.in' configure.in:6: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated. For more info, see: configure.in:6: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation
configure.in:13: installing './compile' configure.in:6: installing './install-sh' configure.in:6: installing './missing' Makefile.am: installing './depcomp' automake: warning: autoconf input should be named 'configure.ac', not 'configure.in'

注:在這個地方有可能會提示config.h.in未找到,先運行一下autoheader,再運行automake --add-missing

-> ls

aclocal.m4 autoscan.log configure depcomp install-sh Makefile.in
autom4te.cache compile configure.in hello.c Makefile.am missing

 

注:zutoheader命令,負責生成config.h文件。該工具通常會從acconfig.h文件中復制用戶附加的符號定義,因為這里沒有附加符號定義,所以不需要創建acconfig.h文件。

 

9. 執行命令 ./configure,生成Makefile

root@localhost:autotools# ./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 whether make supports nested variables... yes checking for gcc... gcc 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 gcc accepts -g... yes checking for gcc option to accept ISO C89... none needed checking whether gcc understands -c and -o together... yes checking for style of include used by make... GNU checking dependency style of gcc... gcc3 checking that generated files are newer than configure... done configure: creating ./config.status config.status: creating Makefile config.status: executing depfiles commands root@localhost:autotools# ls aclocal.m4 autoscan.log config.log configure depcomp install-sh Makefile.am missing autom4te.cache compile config.status configure.in  hello.c  Makefile    Makefile.in

 

 

10. 執行命令make all,即可生成對應的hello可執行程序。

make install ---把該程序安裝到系統中

make clean --清除編譯生成的可執行文件和目標文件

make dist --將程序和相關文檔打包成一個壓縮文檔,以便發布

 

 

 

 

具體流程如下圖所示

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM