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

Koma_Wong發表於2018-10-20

目錄

流程

一.安裝包tar.gz的生成

準備原始檔

autoscan

生成configure.scan

aclocal

autoconf

autoheader

Makefile.am

automake

./configure

make dist \ make distclean

生成tar.gz安裝包

二.安裝測試

解壓tar.gz

./configure

make

make install

執行

三.整體過程回顧

四.後續

本文全部程式碼、資源地址:


流程

一.安裝包tar.gz的生成

準備原始檔

首先準備好原始檔(夾),如下(一下檔案可見https://blog.csdn.net/Rong_Toa/article/details/82716420):

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

0 directories, 3 files

autoscan

生成configure.scan

然後進入資料夾,執行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, rongtoa@163.com)
  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

執行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指令:

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

autoheader

執行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

接下來建立檔案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

$ 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      install-sh@  Makefile.in
2048.exe*   autoscan-2.69.log  config.status*  main.c*      missing@
2048.h*     compile@           configure*      main.o       stamp-h1
2048.o      config.h           configure.ac    Makefile
aclocal.m4  config.h.in        depcomp@        Makefile.am

./configure

接下來進行./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 distclean

$ 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

生成tar.gz安裝包

生成了一個:

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

進入資料夾進行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
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 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

三.整體過程回顧

$ cd 2048-c/
$ ls
2048.c*  2048.h*  main.c*

$ ls
2048.c*  2048.h*  main.c*

$ autoscan
輸出略...

$ ls
2048.c*  2048.h*  autoscan-2.69.log  configure.scan  main.c*

$ mv configure.scan configure.ac

$ ls
2048.c*  2048.h*  autoscan-2.69.log  configure.ac  main.c*

$ vim configure.ac
configure.ac 內容如下:
  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.0, rongtoa@163.com)
  6 AM_INIT_AUTOMAKE(2048, 1.0.0)
  7 AC_CONFIG_SRCDIR([2048.c])
  8 AC_CONFIG_HEADERS([config.h])
  9 AC_CONFIG_FILES([Makefile])
 10
 11 # Checks for programs.
 12 AC_PROG_CC
 13
 14 # Checks for libraries.
 15
 16 # Checks for header files.
 17 AC_CHECK_HEADERS([stdlib.h string.h termios.h unistd.h])
 18
 19 # Checks for typedefs, structures, and compiler characteristics.
 20
 21 # Checks for library functions.
 22
 23 AC_OUTPUT
:wq

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

$ aclocal

$ autoconf

$ autoheader

$ 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
configure.ac:12: installing './compile'
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
Makefile.am: installing './depcomp'

$ ./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
略去部分中間輸出...
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands

$ make
make  all-am
make[1]: 進入目錄“/home/rongtao/2048-c”
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-c”

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

$ make clean
test -z "2048.exe" || rm -f 2048.exe
rm -f *.o

$ 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
2048.c*            autom4te.cache/    configure*    main.c*
2048.h*            autoscan-2.69.log  configure.ac  Makefile.am
2048-1.0.0.tar.gz  compile@           depcomp@      Makefile.in
aclocal.m4         config.h.in        install-sh@   missing@

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

$ cd 2048-1.0.0

$ ./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 that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands

$ make
make  all-am
make[1]: 進入目錄“/home/rongtao/2048-c/2048-1.0.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-c/2048-1.0.0”

$ make install
make[1]: 進入目錄“/home/rongtao/2048-c/2048-1.0.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-c/2048-1.0.0”

四.後續

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

本文全部程式碼、資源地址:

https://download.csdn.net/download/rong_toa/10733319

相關文章