日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

autotools 自动编译系列简介

發布時間:2025/3/15 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 autotools 自动编译系列简介 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

autotools安裝

?

1.介紹


Linux環境下,我們編譯程序啥的都是一般用的GCC&&GDB等等工具,直接使用GCC命令進行編譯操作。這種方式一般是適用于程序文件比較少,組織結構比較簡單的情況。但是,當我們程序文件比較的多的時候,或者是程序文件組織結構比較的復雜(例如在程序文件夾中存在文件夾多層嵌套以及復雜引用等),此時我們如果是直接使用GCC一點一點的編譯工作量會非常的大,而且萬一程序修改了,還要重新的再工作一遍。為此,我們有了make工具,依靠Makefile輔助文件,我們可以方便的進行工程的管理,以及編譯操作。當程序很復雜的時候,依靠我們去手工的建立、維護Makefile文件是非常的不現實的,不僅很復雜,而且費時費力,還容易出錯。為此,就有了我們的_Autotools_工具或者 CMake 工具棧,而本文重點介紹的是 Autotool 管理 Makefile 的工具鏈。autotools 只要輸入工程中的目標文件、依賴文件、文件目錄等信息就可以自動生成Makefile。這時使用autotools工具就是一個不錯的選擇,只要輸入工程中的目標文件、依賴文件、文件目錄等信息就可以自動生成Makefile。autotools工具是個系列工具,主要有:aclocal、autoscan、autoconf、autoheader、automake。

?

2.autotools組成

名稱功能
autoscan?autoscan是用來掃描源代碼目錄生成configure.scan文件的。?? ?configure.scan包含了系統配置的基本選項,里面都是一些宏定義。我們需要將它改名為configure.in
aclocal?aclocal是一個perl腳本程序。aclocal根據configure.in文件的內容,自動生成aclocal.m4文件。aclocal的定義是:“aclocal - create aclocal.m4 by scanning configure.ac”。?? ?生成的aclocal.m4是宏展開文件
autoconfautoconf是用來產生configure文件的??? ?configure.in文件的內容是一些宏,這些宏經過autoconf處理后會變成檢查系統特性、環境變量、軟件必須的參數的shell腳本
autoheader?自動生成config.h.in?? ?在configure生成config.h時候的in文件
automake我們使用automake --add-missing來產生Makefile.in??? ?Makefile.am是用來生成Makefile.in的,需要你手工書寫

?

3.autotools處理流程


Autotools使用流程:
1.目錄樹的最高層運行autoscan,生成configure.scan文件;
2.運行aclocal,生成aclocal.m4文件;
3.運行autoconf,生成configure配置腳本;
4.運行autoheader,生成config.h.in文件;
5.手工編寫Makefile.am文件;
6.運行automake,生成Makefile.in;
7.運行配置腳本configure,生成Makefile。

?

4.autotools安裝


[root@localhost ~]# yum install automake
安裝包:

automake-1.13.4-3.el7.noarch.rpm
autoconf-2.69-11.el7.noarch.rpm
perl-Test-Harness-3.28-3.el7.noarch.rpm
m4-1.4.16-10.el7.x86_64.rpm
?

autotools實例

1.生成源碼,并且確認代碼可以編譯執行

[root@localhost ~]#mkdir /home/mycode/auto_make_test/ [root@localhost ~]# cd /home/mycode/auto_make_test/ [root@localhost auto_make_test]# ll total 0 [root@localhost auto_make_test]# vim auto_test.c [root@localhost auto_make_test]# cat auto_test.c? #include <stdio.h>int main() {printf("auto make test\n");return 0; } [root@localhost auto_make_test]# gcc auto_test.c? [root@localhost auto_make_test]# ll total 16 -rwxr-xr-x 1 root root 8560 Jan ?5 17:01 a.out -rw-r--r-- 1 root root ? 81 Jan ?5 17:01 auto_test.c [root@localhost auto_make_test]# ./a.out? auto make test [root@localhost auto_make_test]# rm a.out? rm: remove regular file ‘a.out’? y

2.執行autoscan


第一步:在源碼目錄下執行autoscan命令。這個命令主要用于掃描工作目錄,并且生成configure.scan文件。configure.scan需要重命令成configure.ac,然后編輯這個配置,我們才能繼續執行后面的命令。

[root@localhost auto_make_test]# autoscan? [root@localhost auto_make_test]# ll total 8 -rw-r--r-- 1 root root ? 0 Jan ?5 17:35 autoscan.log -rw-r--r-- 1 root root ?81 Jan ?5 17:01 auto_test.c -rw-r--r-- 1 root root 471 Jan ?5 17:35 configure.scan [root@localhost auto_make_test]# mv configure.scan configure.ac

第二步:編輯上面得到的configure.ac文件。

[root@localhost auto_make_test]# cat configure.ac? # ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -*- Autoconf -*- # Process this file with autoconf to produce a configure script.AC_PREREQ([2.69]) AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS]) AC_CONFIG_SRCDIR([auto_test.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 [root@localhost auto_make_test]# vim configure.ac? [root@localhost auto_make_test]# cat configure.ac? # ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -*- Autoconf -*- # Process this file with autoconf to produce a configure script.AC_PREREQ([2.69]) AC_INIT([auto_test], [1.0], [751773517@qq.com]) AC_CONFIG_SRCDIR([auto_test.c]) AC_CONFIG_HEADERS([config.h]) AM_INIT_AUTOMAKE(auto_test,1.0)# 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_INIT,AC_OUTPUT
增加了:AM_INIT_AUTOMAKE(auto_test,1.0)
configure.ac標簽說明如下表:

標簽功能
AC_PREREQ聲明autoconf要求的版本號。
AC_INIT?定義軟件名稱、版本號、聯系方式
AM_INIT_AUTOMAKE???必須要的,參數為軟件名稱和版本號
AC_CONFIG_SCRDIR?宏用來偵測所指定的源碼文件是否存在, 來確定源碼目錄的有效性.。此處為當前目錄下main.c
AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader命令使用。
AC_PROG_CC?指定編譯器,默認是GCC
AC_CONFIG_FILES生成相應的Makefile文件,不同文件夾下的Makefile通過空格分隔。例如:AC_CONFIG_FILES([Makefile, src/Makefile])
AC_OUTPUT用來設定 configure 所要產生的文件,如果是makefile,configure 會把它檢查出來的結果帶入makefile.in文件產生合適的makefile。

?? ?
?

3.執行aclocal??

執行aclocal命令,掃描 configure.ac 文件生成 aclocal.m4文件, 該文件主要處理本地的宏定義,它根據已經安裝的宏、用戶定義宏和 acinclude.m4 文件中的宏將 configure.ac 文件需要的宏集中定義到文件 aclocal.m4 中。

[root@localhost auto_make_test]# aclocal [root@localhost auto_make_test]# ll total 52 -rw-r--r-- 1 root root 37794 Jan ?5 17:43 aclocal.m4 drwxr-xr-x 2 root root ?4096 Jan ?5 17:43 autom4te.cache -rw-r--r-- 1 root root ? ? 0 Jan ?5 17:42 autoscan.log -rw-r--r-- 1 root root ? ?81 Jan ?5 17:01 auto_test.c -rw-r--r-- 1 root root ? 499 Jan ?5 17:38 configure.ac


?4.執行autoconf


這個命令將 configure.ac 文件中的宏展開,生成 configure 腳本。這個過程可能要用到aclocal.m4中定義的宏。

[root@localhost auto_make_test]# autoconf [root@localhost auto_make_test]# ll total 196 -rw-r--r-- 1 root root ?37794 Jan ?5 17:43 aclocal.m4 drwxr-xr-x 2 root root ? 4096 Jan ?5 17:43 autom4te.cache -rw-r--r-- 1 root root ? ? ?0 Jan ?5 17:42 autoscan.log -rw-r--r-- 1 root root ? ? 81 Jan ?5 17:01 auto_test.c -rwxr-xr-x 1 root root 141850 Jan ?5 17:43 configure -rw-r--r-- 1 root root ? ?499 Jan ?5 17:38 configure.ac

5.執行autoheader


該命令生成 config.h.in 文件。該命令通常會從 "acconfig.h”文件中復制用戶附加的符號定義。該例子中沒有附加的符號定義, 所以不需要創建 "acconfig.h”文件。

[root@localhost auto_make_test]# autoheader? [root@localhost auto_make_test]# ll total 200 -rw-r--r-- 1 root root ?37794 Jan ?5 17:43 aclocal.m4 drwxr-xr-x 2 root root ? 4096 Jan ?5 17:43 autom4te.cache -rw-r--r-- 1 root root ? ? ?0 Jan ?5 17:42 autoscan.log -rw-r--r-- 1 root root ? ? 81 Jan ?5 17:01 auto_test.c -rw-r--r-- 1 root root ? ?625 Jan ?5 17:43 config.h.in -rwxr-xr-x 1 root root 141850 Jan ?5 17:43 configure -rw-r--r-- 1 root root ? ?499 Jan ?5 17:38 configure.ac

6、創建Makefile.am文件:


Automake工具會根據 configure.in 中的參量把 Makefile.am 轉換成 Makefile.in 文件。最終通過Makefile.in生成Makefile文件,所以Makefile.am這個文件非常重要,定義了一些生成Makefile的規則。

[root@localhost auto_make_test]# vim Makefile.am [root@localhost auto_make_test]# cat Makefile.am? AUTOMAKE_OPTIONS=foreign bin_PROGRAMS=auto_test auto_test_SOURCES=auto_test.c

1). AUTOMAKE_OPTIONS:由于GNU對自己發布的軟件有嚴格的規范, 比如必須附帶許可證聲明文件COPYING等,否則automake執行時會報錯. automake提供了3中軟件等級:foreign, gnu和gnits, 供用戶選擇。默認級別是gnu。在本例中,使用了foreign等級, 它只檢測必須的文件。
2). bin_PROGRAMS = auto_test:生成的可執行文件名稱,生成多個可執行文件,可以用空格隔開。
3). auto_test_SOURCES:生成可執行文件auto_test需要依賴的源文件。其中auto_test_為可執行文件的名稱。


7、執行automake

執行automake --add-missing命令。該命令生成 Makefile.in 文件。使用選項 "--add-missing" 可以讓Automake自動添加一些必需的腳本文件。如果發現一些文件不存在,可以通過手工 touch命令創建。

[root@localhost auto_make_test]# ll total 204 -rw-r--r-- 1 root root ?37794 Jan 15 15:30 aclocal.m4 drwxr-xr-x 2 root root ? 4096 Jan 15 15:30 autom4te.cache -rw-r--r-- 1 root root ? ? ?0 Jan 15 15:29 autoscan.log -rw-r--r-- 1 root root ? ? 81 Jan ?5 17:01 auto_test.c -rw-r--r-- 1 root root ? ?625 Jan 15 15:31 config.h.in -rwxr-xr-x 1 root root 141850 Jan 15 15:30 configure -rw-r--r-- 1 root root ? ?499 Jan ?5 17:38 configure.ac -rw-r--r-- 1 root root ? ? 78 Jan ?5 17:45 Makefile.am [root@localhost auto_make_test]# automake --add-missing configure.ac:8: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated. ?For more info, see: configure.ac:8: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation configure.ac:8: installing './install-sh' configure.ac:8: installing './missing' Makefile.am: installing './depcomp' [root@localhost auto_make_test]# ll total 228 -rw-r--r-- 1 root root ?37794 Jan 15 15:30 aclocal.m4 drwxr-xr-x 2 root root ? 4096 Jan 15 15:32 autom4te.cache -rw-r--r-- 1 root root ? ? ?0 Jan 15 15:29 autoscan.log -rw-r--r-- 1 root root ? ? 81 Jan ?5 17:01 auto_test.c -rw-r--r-- 1 root root ? ?625 Jan 15 15:31 config.h.in -rwxr-xr-x 1 root root 141850 Jan 15 15:30 configure -rw-r--r-- 1 root root ? ?499 Jan ?5 17:38 configure.ac lrwxrwxrwx 1 root root ? ? 32 Jan 15 15:32 depcomp -> /usr/share/automake-1.13/depcomp lrwxrwxrwx 1 root root ? ? 35 Jan 15 15:32 install-sh -> /usr/share/automake-1.13/install-sh -rw-r--r-- 1 root root ? ? 78 Jan ?5 17:45 Makefile.am -rw-r--r-- 1 root root ?23321 Jan 15 15:32 Makefile.in lrwxrwxrwx 1 root root ? ? 32 Jan 15 15:32 missing -> /usr/share/automake-1.13/missing [root@localhost auto_make_test]#?

8、執行configure:


執行configure生成Makefile文件,便可以運行make生成可執行文件了。

[root@localhost auto_make_test]# ./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... /usr/bin/mkdir -p checking for gawk... gawk -rw-r--r-- 1 root root ? ?625 Jan 15 15:31 config.h.in -rw-r--r-- 1 root root ? 8546 Jan 15 15:34 config.log -rwxr-xr-x 1 root root ?32482 Jan 15 15:34 config.status -rwxr-xr-x 1 root root 141850 Jan 15 15:30 configure -rw-r--r-- 1 root root ? ?499 Jan ?5 17:38 configure.ac lrwxrwxrwx 1 root root ? ? 32 Jan 15 15:32 depcomp -> /usr/share/automake-1.13/depcomp lrwxrwxrwx 1 root root ? ? 35 Jan 15 15:32 install-sh -> /usr/share/automake-1.13/install-sh -rw-r--r-- 1 root root ?23092 Jan 15 15:34 Makefile -rw-r--r-- 1 root root ? ? 78 Jan ?5 17:45 Makefile.am -rw-r--r-- 1 root root ?23321 Jan 15 15:32 Makefile.in lrwxrwxrwx 1 root root ? ? 32 Jan 15 15:32 missing -> /usr/share/automake-1.13/missing -rw-r--r-- 1 root root ? ? 23 Jan 15 15:34 stamp-h1 [root@localhost auto_make_test]# make make all-am make[1]: Entering directory `/home/mycode/auto_make_test' gcc -DHAVE_CONFIG_H -I. -g -O2 -MT auto_test.o -MD -MP -MF .deps/auto_test.Tpo -c -o auto_test.o auto_test.c mv -f .deps/auto_test.Tpo .deps/auto_test.Po gcc -g -O2 -o auto_test auto_test.o make[1]: Leaving directory `/home/mycode/auto_make_test' [root@localhost auto_make_test]# ll total 324 -rw-r--r-- 1 root root 37794 Jan 15 15:30 aclocal.m4 drwxr-xr-x 2 root root 4096 Jan 15 15:32 autom4te.cache -rw-r--r-- 1 root root 0 Jan 15 15:29 autoscan.log -rwxr-xr-x 1 root root 11048 Jan 15 15:34 auto_test -rw-r--r-- 1 root root 81 Jan 5 17:01 auto_test.c -rw-r--r-- 1 root root 5984 Jan 15 15:34 auto_test.o -rw-r--r-- 1 root root 778 Jan 15 15:34 config.h -rw-r--r-- 1 root root 625 Jan 15 15:31 config.h.in -rw-r--r-- 1 root root 8546 Jan 15 15:34 config.log -rwxr-xr-x 1 root root 32482 Jan 15 15:34 config.status -rwxr-xr-x 1 root root 141850 Jan 15 15:30 configure -rw-r--r-- 1 root root 499 Jan 5 17:38 configure.ac lrwxrwxrwx 1 root root 32 Jan 15 15:32 depcomp -> /usr/share/automake-1.13/depcomp lrwxrwxrwx 1 root root 35 Jan 15 15:32 install-sh -> /usr/share/automake-1.13/install-sh -rw-r--r-- 1 root root 23092 Jan 15 15:34 Makefile -rw-r--r-- 1 root root 78 Jan 5 17:45 Makefile.am -rw-r--r-- 1 root root 23321 Jan 15 15:32 Makefile.in lrwxrwxrwx 1 root root 32 Jan 15 15:32 missing -> /usr/share/automake-1.13/missing -rw-r--r-- 1 root root 23 Jan 15 15:34 stamp-h1 [root@localhost auto_make_test]# ./auto_test auto make test [root@localhost auto_make_test]#

此外還可以執行make的其他操作比如打包、install、uninstall等:
?

[root@localhost auto_make_test]# make dist make ?dist-gzip am__post_remove_distdir='@:' make[1]: Entering directory `/home/mycode/auto_make_test' if test -d "auto_test-1.0"; then find "auto_test-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -rf "auto_test-1.0" || { sleep 5 && rm -rf "auto_test-1.0"; }; else :; fi test -d "auto_test-1.0" || mkdir "auto_test-1.0" test -n "" \ || find "auto_test-1.0" -type d ! -perm -755 \-exec chmod u+rwx,go+rx {} \; -o \! -type d ! -perm -444 -links 1 -exec chmod a+r {} \; -o \! -type d ! -perm -400 -exec chmod a+r {} \; -o \! -type d ! -perm -444 -exec /bin/sh /home/mycode/auto_make_test/install-sh -c -m a+r {} {} \; \ || chmod -R a+r "auto_test-1.0" tardir=auto_test-1.0 && ${TAR-tar} chof - "$tardir" | GZIP=--best gzip -c >auto_test-1.0.tar.gz make[1]: Leaving directory `/home/mycode/auto_make_test' if test -d "auto_test-1.0"; then find "auto_test-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -rf "auto_test-1.0" || { sleep 5 && rm -rf "auto_test-1.0"; }; else :; fi [root@localhost auto_make_test]# ll total 400 -rw-r--r-- 1 root root ?37794 Jan 15 15:30 aclocal.m4 drwxr-xr-x 2 root root ? 4096 Jan 15 15:32 autom4te.cache -rw-r--r-- 1 root root ? ? ?0 Jan 15 15:29 autoscan.log -rwxr-xr-x 1 root root ?11048 Jan 15 15:34 auto_test -rw-r--r-- 1 root root ?71596 Jan 15 15:37 auto_test-1.0.tar.gz -rw-r--r-- 1 root root ? ? 81 Jan ?5 17:01 auto_test.c -rw-r--r-- 1 root root ? 5984 Jan 15 15:34 auto_test.o -rw-r--r-- 1 root root ? ?778 Jan 15 15:34 config.h -rw-r--r-- 1 root root ? ?625 Jan 15 15:31 config.h.in -rw-r--r-- 1 root root ? 8546 Jan 15 15:34 config.log -rwxr-xr-x 1 root root ?32482 Jan 15 15:34 config.status

?

autogen.sh實例

之前介紹的automake貌似工序過于復雜,在這里其實是沒有必要做這么復雜的工作的,完全可以將其抽象成一個模板性質的腳本將各個工序都集中到腳本里面,使用者只需要稍微修改自己的配置文件即可,直白點就是使用autogen.sh,相信這個腳本在很多的開源代碼甚至網上的帖子討論中都已經泛濫了。在此也使用這個腳本,然后再代碼中只需要補充上configure.ac和Makefile.am文件即可。具體流程如下所示:

1.準備文件

[root@localhost ~]# mkdir autogen_test [root@localhost ~]# cd autogen_test/ [root@localhost autogen_test]# mkdir src? [root@localhost autogen_test]# cd src/


測試代碼:

[root@localhost src]# vim auto_test.c [root@localhost src]# cat auto_test.c #include <stdio.h>int main() {printf("auto make test\n");return 0; }


編寫源碼的Makefile.am

[root@localhost src]# vim Makefile.am [root@localhost src]# cat Makefile.am? bin_PROGRAMS=auto_test auto_test_SOURCES=auto_test.c auto_test_LDADD= LIBS=-lm INCLUDES=-I/usr/include


?編寫外層目錄Makefile.am

[root@localhost src]# cd .. [root@localhost autogen_test]# vim Makefile.am? [root@localhost autogen_test]# cat Makefile.am? SUBDIRS = src.PHONY: auto_cleanauto_clean: distcleanfind . -name Makefile.in -exec rm -f {} \;rm -rf autom4te.cacherm -f missing aclocal.m4 config.h.in config.guess config.sub ltmain.sh install-sh configure depcomp compile


?編寫autogen.sh文件:

[root@localhost autogen_test]# vim autogen.sh? [root@localhost autogen_test]# cat autogen.sh? #!/bin/shecho echo ... auto_test autogen ... echo## Check all dependencies are present MISSING=""# Check for aclocal env aclocal --version > /dev/null 2>&1 if [ $? -eq 0 ]; thenACLOCAL=aclocal elseMISSING="$MISSING aclocal" fi# Check for autoconf env autoconf --version > /dev/null 2>&1 if [ $? -eq 0 ]; thenAUTOCONF=autoconf elseMISSING="$MISSING autoconf" fi# Check for autoheader env autoheader --version > /dev/null 2>&1 if [ $? -eq 0 ]; thenAUTOHEADER=autoheader elseMISSING="$MISSING autoheader" fi# Check for automake env automake --version > /dev/null 2>&1 if [ $? -eq 0 ]; thenAUTOMAKE=automake elseMISSING="$MISSING automake" fi# Check for libtoolize or glibtoolize env libtoolize --version > /dev/null 2>&1 if [ $? -eq 0 ]; then# libtoolize was found, so use itTOOL=libtoolize else# libtoolize wasn't found, so check for glibtoolizeenv glibtoolize --version > /dev/null 2>&1if [ $? -eq 0 ]; thenTOOL=glibtoolizeelseMISSING="$MISSING libtoolize/glibtoolize"fi fi# Check for tar env tar -cf /dev/null /dev/null > /dev/null 2>&1 if [ $? -ne 0 ]; thenMISSING="$MISSING tar" fi## If dependencies are missing, warn the user and abort if [ "x$MISSING" != "x" ]; thenecho "Aborting."echoecho "The following build tools are missing:"echofor pkg in $MISSING; doecho " ?* $pkg"doneechoecho "Please install them and try again."echoexit 1 fi## Do the autogeneration echo Running ${ACLOCAL}... $ACLOCAL? echo Running ${AUTOHEADER}... $AUTOHEADER echo Running ${TOOL}... $TOOL --automake --copy --force echo Running ${AUTOCONF}... $AUTOCONF echo Running ${AUTOMAKE}... $AUTOMAKE --add-missing --force-missing --copy --foreign# Run autogen in the argp-standalone sub-directory #echo "Running autogen.sh in argp-standalone ..." #( cd contrib/argp-standalone;./autogen.sh )# Instruct user on next steps echo echo "Please proceed with configuring, compiling, and installing."


編寫configure.ac文件

[root@localhost autogen_test]# vim configure.ac? [root@localhost autogen_test]# cat configure.ac? # ? ? ? ? ? ? ? ? ? ? -*- Autoconf -*- # Process this file with autoconf to produce a configure script.AC_PREREQ([2.69]) AC_INIT([auto_test],[1.0],[751772517@qq.com])AC_SUBST([PACKAGE_RELEASE],[1.0],[751772517@qq.com])AM_INIT_AUTOMAKE(auto_test,1.0) AC_CONFIG_SRCDIR([src/auto_test.c]) AC_CONFIG_HEADERS([config.h]) AC_CONFIG_FILES([Makefilesrc/Makefile])# Checks for programs. AC_PROG_CC AC_PROG_LIBTOOL# Checks for libraries.# Checks for header files. AC_CHECK_HEADERS([stddef.h string.h])# Checks for typedefs, structures, and compiler characteristics. AC_HEADER_STDBOOL AC_TYPE_SIZE_T# Checks for library functions. AC_FUNC_REALLOC AC_CHECK_FUNCS([memset socket])AC_OUTPUT


文件結構:

[root@localhost autogen_test]# tree . |-- autogen.sh |-- configure.ac |-- Makefile.am `-- src|-- auto_test.c`-- Makefile.am1 directory, 5 files [root@localhost autogen_test]#

截止到這里所有的文件都已經創建好了,也就是說前期的準備就可以了,后續只需要運行腳本就可以生成Makefile文件了

2.生成Makefile

[root@localhost autogen_test]# ./autogen.sh?... auto_test autogen ...Running aclocal... Running autoheader... Running libtoolize... Running autoconf... Running automake... configure.ac:9: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated. ?For more info, see: configure.ac:9: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation configure.ac:17: installing './config.guess' configure.ac:17: installing './config.sub' configure.ac:9: installing './install-sh' configure.ac:9: installing './missing' src/Makefile.am:5: warning: 'INCLUDES' is the old name for 'AM_CPPFLAGS' (or '*_CPPFLAGS') src/Makefile.am: installing './depcomp'Please proceed with configuring, compiling, and installing.


查看文件:

[root@localhost autogen_test]# ll total 1224 -rw-r--r-- 1 root root 346593 Jan 19 10:47 aclocal.m4 -rwxr-xr-x 1 root root ? 1986 Jan 18 15:22 autogen.sh drwxr-xr-x 2 root root ? 4096 Jan 19 10:47 autom4te.cache -rwxr-xr-x 1 root root ?45297 Jan 19 10:47 config.guess -rw-r--r-- 1 root root ? 2278 Jan 19 10:47 config.h.in -rwxr-xr-x 1 root root ?35533 Jan 19 10:47 config.sub -rwxr-xr-x 1 root root 415594 Jan 19 10:47 configure -rwxr-xr-x 1 root root ? ?708 Jan 18 16:03 configure.ac -rwxr-xr-x 1 root root ?23566 Jan 19 10:47 depcomp -rwxr-xr-x 1 root root ?13997 Jan 19 10:47 install-sh -rw-r--r-- 1 root root 283474 Jan 19 10:47 ltmain.sh -rw-r--r-- 1 root root ? ?229 Jan 19 10:45 Makefile.am -rw-r--r-- 1 root root ?24704 Jan 19 10:47 Makefile.in -rwxr-xr-x 1 root root ? 6873 Jan 19 10:47 missing drwxr-xr-x 2 root root ? 4096 Jan 19 10:47 src


?執行configure

[root@localhost autogen_test]# ./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... /usr/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 for style of include used by make... GNU checking dependency style of gcc... gcc3 checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking how to print strings... printf checking for a sed that does not truncate output... /usr/bin/sed checking for grep that handles long lines and -e... /usr/bin/grep checking for egrep... /usr/bin/grep -E checking for fgrep... /usr/bin/grep -F checking for ld used by gcc... /usr/bin/ld checking if the linker (/usr/bin/ld) is GNU ld... yes checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B checking the name lister (/usr/bin/nm -B) interface... BSD nm checking whether ln -s works... yes checking the maximum length of command line arguments... 1572864 checking whether the shell understands some XSI constructs... yes checking whether the shell understands "+="... yes checking how to convert x86_64-unknown-linux-gnu file names to x86_64-unknown-linux-gnu format... func_convert_file_noop checking how to convert x86_64-unknown-linux-gnu file names to toolchain format... func_convert_file_noop checking for /usr/bin/ld option to reload object files... -r checking for objdump... objdump checking how to recognize dependent libraries... pass_all checking for dlltool... no checking how to associate runtime and link libraries... printf %s\n checking for ar... ar checking for archiver @FILE support... @ checking for strip... strip checking for ranlib... ranlib checking command to parse /usr/bin/nm -B output from gcc object... ok checking for sysroot... no checking for mt... no checking if : is a manifest tool... no checking how to run the C preprocessor... gcc -E checking for ANSI C header files... yes checking for sys/types.h... yes checking for sys/stat.h... yes checking for stdlib.h... yes checking for string.h... yes checking for memory.h... yes checking for strings.h... yes checking for inttypes.h... yes checking for stdint.h... yes checking for unistd.h... yes checking for dlfcn.h... yes checking for objdir... .libs checking if gcc supports -fno-rtti -fno-exceptions... no checking for gcc option to produce PIC... -fPIC -DPIC checking if gcc PIC flag -fPIC -DPIC works... yes checking if gcc static flag -static works... no checking if gcc supports -c -o file.o... yes checking if gcc supports -c -o file.o... (cached) yes checking whether the gcc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes checking whether -lc should be explicitly linked in... no checking dynamic linker characteristics... GNU/Linux ld.so checking how to hardcode library paths into programs... immediate checking whether stripping libraries is possible... yes checking if libtool supports shared libraries... yes checking whether to build shared libraries... yes checking whether to build static libraries... yes checking stddef.h usability... yes checking stddef.h presence... yes checking for stddef.h... yes checking for string.h... (cached) yes checking for stdbool.h that conforms to C99... yes checking for _Bool... yes checking for size_t... yes checking for stdlib.h... (cached) yes checking for GNU libc compatible realloc... yes checking for memset... yes checking for socket... yes checking that generated files are newer than configure... done configure: creating ./config.status config.status: creating Makefile config.status: creating src/Makefile config.status: creating config.h config.status: executing depfiles commands config.status: executing libtool commands


?查看生成的文件

[root@localhost autogen_test]# ll total 1644 -rw-r--r-- 1 root root 346593 Jan 19 10:47 aclocal.m4 -rwxr-xr-x 1 root root ? 1986 Jan 18 15:22 autogen.sh drwxr-xr-x 2 root root ? 4096 Jan 19 10:47 autom4te.cache -rwxr-xr-x 1 root root ?45297 Jan 19 10:47 config.guess -rw-r--r-- 1 root root ? 2504 Jan 19 10:48 config.h -rw-r--r-- 1 root root ? 2278 Jan 19 10:47 config.h.in -rw-r--r-- 1 root root ?25756 Jan 19 10:48 config.log -rwxr-xr-x 1 root root ?61429 Jan 19 10:48 config.status -rwxr-xr-x 1 root root ?35533 Jan 19 10:47 config.sub -rwxr-xr-x 1 root root 415594 Jan 19 10:47 configure -rwxr-xr-x 1 root root ? ?708 Jan 18 16:03 configure.ac -rwxr-xr-x 1 root root ?23566 Jan 19 10:47 depcomp -rwxr-xr-x 1 root root ?13997 Jan 19 10:47 install-sh -rwxr-xr-x 1 root root 292999 Jan 19 10:48 libtool -rw-r--r-- 1 root root 283474 Jan 19 10:47 ltmain.sh -rw-r--r-- 1 root root ?24843 Jan 19 10:48 Makefile -rw-r--r-- 1 root root ? ?229 Jan 19 10:45 Makefile.am -rw-r--r-- 1 root root ?24704 Jan 19 10:47 Makefile.in -rwxr-xr-x 1 root root ? 6873 Jan 19 10:47 missing drwxr-xr-x 3 root root ? 4096 Jan 19 10:48 src -rw-r--r-- 1 root root ? ? 23 Jan 19 10:48 stamp-h1 [root@localhost autogen_test]#?


到這里就生成了Makefile文件,所有的都是直接運行腳本自動生成的。

3.編譯 測試

?

[root@localhost autogen_test]# make? make ?all-recursive make[1]: Entering directory `/home/mycode/autogen_test' Making all in src make[2]: Entering directory `/home/mycode/autogen_test/src' gcc -DHAVE_CONFIG_H -I. -I.. -I/usr/include ? ?-g -O2 -MT auto_test.o -MD -MP -MF .deps/auto_test.Tpo -c -o auto_test.o auto_test.c mv -f .deps/auto_test.Tpo .deps/auto_test.Po /bin/sh ../libtool ?--tag=CC ? --mode=link gcc ?-g -O2 ? -o auto_test auto_test.o ?-lm libtool: link: gcc -g -O2 -o auto_test auto_test.o ?-lm make[2]: Leaving directory `/home/mycode/autogen_test/src' make[2]: Entering directory `/home/mycode/autogen_test' make[2]: Leaving directory `/home/mycode/autogen_test' make[1]: Leaving directory `/home/mycode/autogen_test'


查看生成的可執行文件:

[root@localhost autogen_test]# ll src/ total 68 -rwxr-xr-x 1 root root 11048 Jan 19 10:49 auto_test -rw-r--r-- 1 root root ? ?81 Jan 18 15:11 auto_test.c -rw-r--r-- 1 root root ?5992 Jan 19 10:49 auto_test.o -rw-r--r-- 1 root root 18522 Jan 19 10:48 Makefile -rw-r--r-- 1 root root ? 103 Jan 18 15:16 Makefile.am -rw-r--r-- 1 root root 18875 Jan 19 10:47 Makefile.in


?執行可執行程序:

[root@localhost autogen_test]# ./src/auto_test? auto make test 執行make auto_clean[root@localhost autogen_test]# make auto_clean Making distclean in src make[1]: Entering directory `/home/mycode/autogen_test/src'rm -f auto_test rm -rf .libs _libs rm -f *.o rm -f *.lo rm -f *.tab.c test -z "" || rm -f? test . = "." || test -z "" || rm -f? rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags rm -rf ./.deps rm -f Makefile make[1]: Leaving directory `/home/mycode/autogen_test/src' make[1]: Entering directory `/home/mycode/autogen_test' rm -rf .libs _libs rm -f *.lo test -z "" || rm -f? test . = "." || test -z "" || rm -f? rm -f config.h stamp-h1 rm -f libtool config.lt rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags rm -f cscope.out cscope.in.out cscope.po.out cscope.files make[1]: Leaving directory `/home/mycode/autogen_test' rm -f config.status config.cache config.log configure.lineno config.status.lineno rm -f Makefile find . -name Makefile.in -exec rm -f {} \; rm -rf autom4te.cache rm -f missing aclocal.m4 config.h.in config.guess config.sub ltmain.sh install-sh configure depcomp compile


?查看清理完畢后的文件:

[root@localhost autogen_test]# tree . |-- autogen.sh |-- configure.ac |-- Makefile.am `-- src|-- auto_test.c`-- Makefile.am1 directory, 5 files [root@localhost autogen_test]#?


測試成功,makefi文件可以正常使用,這里就可以將其抽象成一個模板性質的東西,也就是說以后要用的時候只需要修改配置文件configure.ac以及src/Makefile.am即可,自己的代碼可以任意的填充,當然多目錄的也是如此只需要增加相應的目錄以及對于的Makefile.am文件即可,在根目錄下的Makefile.am把目錄名填寫進去就可以自動執行腳本掃描目錄生成Makefile文件進行編譯了,這種用法相對于前文來說工序是一樣的只不過是將單個的命令封裝到腳本里面了,這樣用起來就方便多了。


————————————————
版權聲明:本文為CSDN博主「kongslly」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/kongshuai19900505/article/details/79104442

總結

以上是生活随笔為你收集整理的autotools 自动编译系列简介的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。