1. 程式人生 > >【configure】如何用automake、autoconf指令生成configure並建立自己的linux tar.gz安裝包【初級篇:簡單建立-測試】

【configure】如何用automake、autoconf指令生成configure並建立自己的linux tar.gz安裝包【初級篇:簡單建立-測試】

$ tree 2048-c/
2048-c/
├── 2048.c
├── 2048.h
└── main.c

0 directories, 3 files

然後進入資料夾,執行autoscan生成configure.scan檔案

$ cd 2048-c/
$ ls
2048.c*  2048.h*  main.c*
$ autoscan
Unescaped left brace in regex is deprecated here (and will be fatal in Perl 5.30), passed through in regex; marked by <-- HERE in m/\${ <-- HERE [^\}]*}/ at /usr/bin/autoscan-2.69 line 361.
$ ls
2048.c*  2048.h*  autoscan-2.69.log  configure.scan  main.c*

將configure.scan檔案更名為configure.ac,並進行編輯:

$ mv configure.scan configure.ac
$ vim configure.ac

修改第5行,新增第6行,第9行選填,並儲存:

  1 #                                               -*- Autoconf -*-
  2 # Process this file with autoconf to produce a configure script.
  3
  4 AC_PREREQ([2.69])
  5 AC_INIT(2048, 1.0, 
[email protected]
) 6 AM_INIT_AUTOMAKE(2048, 1.0) 7 AC_CONFIG_SRCDIR([2048.c]) 8 AC_CONFIG_HEADERS([config.h]) 9 AC_CONFIG_FILES([Makefile]) 10 # Checks for programs. 11 AC_PROG_CC 12 13 # Checks for libraries. 14 15 # Checks for header files. 16 AC_CHECK_HEADERS([stdlib.h string.h termios.h unistd.h]) 17 18 # Checks for typedefs, structures, and compiler characteristics. 19 20 # Checks for library functions. 21 22 AC_OUTPUT ~ :wq

執行aclocal命令:

$ ls
2048.c*  2048.h*  autoscan-2.69.log  configure.ac  main.c*
$ aclocal
$ ls
2048.c*  aclocal.m4       autoscan-2.69.log  main.c*
2048.h*  autom4te.cache/  configure.ac

執行autoconf指令:

$ autoconf
$ ls
2048.c*  aclocal.m4       autoscan-2.69.log  configure.ac
2048.h*  autom4te.cache/  configure*         main.c*

執行autoheader命令:

$ autoheader
$ ls
2048.c*  aclocal.m4       autoscan-2.69.log  configure*    main.c*
2048.h*  autom4te.cache/  config.h.in        configure.ac

接下來建立檔案Makefile.am

內容如下(忽略行號):

$ vim Makefile.am
  1 AUTOMAKE_OPTIONS=foreign
  2 bin_PROGRAMS=2048
  3 2048_SOURCES=2048.c 2048.h main.c
~
~
~
:wq

然後automake

$ automake --add-missing
configure.ac:6: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated.  For more info, see:
configure.ac:6: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation

$ ls
2048.c*     autom4te.cache/    config.log      [email protected]  Makefile.in
2048.exe*   autoscan-2.69.log  config.status*  main.c*      [email protected]
2048.h*     [email protected]           configure*      main.o       stamp-h1
2048.o      config.h           configure.ac    Makefile
aclocal.m4  config.h.in        [email protected]        Makefile.am

接下來進行./configure操作生成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... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
...

$

然後make編譯、make distclean刪除安裝包的無關項,make dist生成安裝的壓縮包tar.gz:

$ make dist
make  dist-gzip am__post_remove_distdir='@:'
make[1]: 進入目錄“/home/rongtao/2048-c”
if test -d "2048-1.0"; then find "2048-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -rf "2048-1.0" || { sleep 5 && rm -rf "2048-1.0"; }; else :; fi
test -d "2048-1.0" || mkdir "2048-1.0"
test -n "" \
|| find "2048-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/rongtao/2048-c/install-sh -c -m a+r {} {} \; \
|| chmod -R a+r "2048-1.0"
tardir=2048-1.0 && ${TAR-tar} chof - "$tardir" | eval GZIP= gzip --best -c >2048-1.0.tar.gz
make[1]: 離開目錄“/home/rongtao/2048-c”
if test -d "2048-1.0"; then find "2048-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -rf "2048-1.0" || { sleep 5 && rm -rf "2048-1.0"; }; else :; fi

$ make distclean
test -z "2048.exe" || rm -f 2048.exe
rm -f *.o
rm -f *.tab.c
test -z "" || rm -f
test . = "." || test -z "" || rm -f
rm -f config.h stamp-h1
rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags
rm -f cscope.out cscope.in.out cscope.po.out cscope.files
rm -f config.status config.cache config.log configure.lineno config.status.lineno
rm -rf ./.deps
rm -f Makefile

生成了一個:

$ ls -l
-rw-r--r--  1 rongtao None  82991 10月 20 09:03 2048-1.0.tar.gz

對生成的tar.gz進行安裝

將2018-1.0.tar.gz拷貝至“下載”目錄,假裝他是下載得到的:

$ pwd
/home/rongtao/下載
$ ls
2048-1.0.tar.gz

解壓:

$ tar -xzvf 2048-1.0.tar.gz
2048-1.0/
2048-1.0/2048.c
2048-1.0/2048.h
2048-1.0/aclocal.m4
2048-1.0/compile
2048-1.0/config.h.in
2048-1.0/configure
2048-1.0/configure.ac
2048-1.0/depcomp
2048-1.0/install-sh
2048-1.0/main.c
2048-1.0/Makefile.am
2048-1.0/Makefile.in
2048-1.0/missing

$ ls
2048-1.0/  2048-1.0.tar.gz

進入資料夾進行configure

$ cd 2048-1.0

$ ls
2048.c*     compile*     configure.ac  main.c*      missing*
2048.h*     config.h.in  depcomp*      Makefile.am
aclocal.m4  configure*   install-sh*   Makefile.in

$ ./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
...

$ 

進行編譯make:

$ make
make  all-am
make[1]: 進入目錄“/home/rongtao/下載/2048-1.0”
gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
mv -f .deps/main.Tpo .deps/main.Po
gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT 2048.o -MD -MP -MF .deps/2048.Tpo -c -o 2048.o 2048.c
mv -f .deps/2048.Tpo .deps/2048.Po
gcc  -g -O2   -o 2048.exe main.o 2048.o
make[1]: 離開目錄“/home/rongtao/下載/2048-1.0”

進行安裝make install

$ make install
make[1]: 進入目錄“/home/rongtao/下載/2048-1.0”
 /usr/bin/mkdir -p '/usr/local/bin'
  /usr/bin/install -c 2048.exe '/usr/local/bin'
make[1]: 對“install-data-am”無需做任何事。
make[1]: 離開目錄“/home/rongtao/下載/2048-1.0”

檢查是否安裝,在終端輸入20,然後按Tab直接跳出可執行檔案,是不是很nice:

$ 2048.exe

到此結束,以後會繼續更新對一個專案的configure操作,敬請期待,我也很期待。。。