菜单开关

周梦康 发表于 2019-12-19 381 次浏览

参考 http://mij.oltrelinux.com/devel/autoconf-automake/

项目目录

.
└── src
    ├── http.c
    ├── http.h
    ├── log.c
    ├── log.h
    ├── mywget.c
    ├── progress.c
    ├── progress.h
    ├── test.php
    ├── url.c
    ├── url.h
    ├── wget.c
    ├── wget.h
    ├── wget_opt.c
    └── wget_opt.h

按照官网提供的步骤来操作 https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/autoconf.html#Making-configure-Scripts
GNU autotools 编译实战笔记

autoscan

$autoscan

$ll
total 8
-rw-r--r-- 1 mengkang.net users    0 Dec 19 10:39 autoscan.log
-rw-r--r-- 1 mengkang.net users  723 Dec 19 10:39 configure.scan
drwxr-xr-x 2 mengkang.net users 4096 Dec 19 10:38 src
$mv configure.scan configure.ac

不少博客介绍改为configure.in,那是比较旧的版本了,以官方最新文档为准

https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/autoconf.html#Writing-Autoconf-Input

vim Makefile.am

AUTOMAKE_OPTIONS = foreign
SUBDIRS = src

vim src/Makefile.am

AM_CFLAGS = --pedantic -Wall -std=gnu99 -O2
bin_PROGRAMS = mywget
mywget_SOURCES = http.c  http.h  log.c  log.h  mywget.c  progress.c  progress.h  test.php  url.c  url.h  wget.c  wget.h  wget_opt.c  wget_opt.h

vim configure.ac

https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/autoconf.html#Autoconf-Language
Arguments should be enclosed within the quote characters ‘[’ and ‘]’, and be separated by commas.

这块很多教程也不规范,我们依然以官方最新文档为准。所有的值都用[] 包裹,不区分值的类型

#                                               -*- 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_INIT([mywget], [0.0.1], [i@mengkang.net])
AC_CONFIG_SRCDIR([src/http.c])
AC_CONFIG_HEADERS([config.h])
+ AM_INIT_AUTOMAKE
# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.
AC_CHECK_HEADERS([arpa/inet.h fcntl.h netdb.h netinet/in.h stdlib.h string.h sys/ioctl.h sys/socket.h unistd.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_CHECK_HEADER_STDBOOL

# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_REALLOC
AC_CHECK_FUNCS([gethostbyname inet_ntoa memset socket strchr strdup strndup strstr])

+ AC_CONFIG_FILES([Makefile src/Makefile])
AC_OUTPUT

aclocal

生成aclocal.m4

This generates a file aclocal.m4 that contains macros for automake things, e.g. AM_INIT_AUTOMAKE.

autoheader

生成config.h.in

automake --add-missing

$automake --add-missing
src/Makefile.am: installing './depcomp'

$ll
total 216
-rw-r--r-- 1 mengkang.net users  37794 Dec 19 12:02 aclocal.m4
drwxr-xr-x 2 mengkang.net users   4096 Dec 19 12:02 autom4te.cache
-rw-r--r-- 1 mengkang.net users      0 Dec 19 10:39 autoscan.log
-rw-r--r-- 1 mengkang.net users   2952 Dec 19 12:00 config.h.in
-rwxr-xr-x 1 mengkang.net users 134679 Dec 19 11:43 configure
-rw-r--r-- 1 mengkang.net users    748 Dec 19 12:02 configure.ac
lrwxrwxrwx 1 mengkang.net users     32 Dec 19 12:02 depcomp -> /usr/share/automake-1.13/depcomp
lrwxrwxrwx 1 mengkang.net users     35 Dec 19 12:02 install-sh -> /usr/share/automake-1.13/install-sh
-rw-r--r-- 1 mengkang.net users     41 Dec 19 11:48 Makefile.am
-rw-r--r-- 1 mengkang.net users  23647 Dec 19 12:02 Makefile.in
lrwxrwxrwx 1 mengkang.net users     32 Dec 19 12:02 missing -> /usr/share/automake-1.13/missing
drwxr-xr-x 2 mengkang.net users   4096 Dec 19 12:02 src

autoconf

完善 configure 脚本的内容

开始我们熟悉的套路了

$ ./configure
$ make
$ sudo make install
$sudo make install
Making install in src
make[1]: Entering directory `/home/mengkang.net/my-wget/10/src'
make[2]: Entering directory `/home/mengkang.net/my-wget/10/src'
 /usr/bin/mkdir -p '/usr/local/bin'
  /usr/bin/install -c mywget '/usr/local/bin'
make[2]: Nothing to be done for `install-data-am'.
make[2]: Leaving directory `/home/mengkang.net/my-wget/10/src'
make[1]: Leaving directory `/home/mengkang.net/my-wget/10/src'
make[1]: Entering directory `/home/mengkang.net/my-wget/10'
make[2]: Entering directory `/home/mengkang.net/my-wget/10'
make[2]: Nothing to be done for `install-exec-am'.
make[2]: Nothing to be done for `install-data-am'.
make[2]: Leaving directory `/home/mengkang.net/my-wget/10'
make[1]: Leaving directory `/home/mengkang.net/my-wget/10'

如上面所述,我们的二进制文件就安装在了/usr/local/bin/mywget,因为/usr/local/bin是在所有用户的环境变量里面的,所以我们可以检测使用下。

检测使用

$mywget
Usage: mywget [OPTION]... [URL]...
Try 'mywget --help' for more information.

$mywget -h
Usage: mywget [OPTION]... [URL]...
  -O,  --output-document=FILE  write documents to FILE
  -d,  --debug                 output debug message
  -h,  --help                  display this help and exit
  -V,  --version               output version information and exit

我们可以在安装的时候使用我们熟悉的其他套路了,比如指定前缀

$ ./configure --prefix=/home/mengkang.net/my-wget/10
...
$ make install
Making install in src
make[1]: Entering directory `/home/mengkang.net/my-wget/10/src'
make[2]: Entering directory `/home/mengkang.net/my-wget/10/src'
 /usr/bin/mkdir -p '/home/mengkang.net/my-wget/10/bin'
  /usr/bin/install -c mywget '/home/mengkang.net/my-wget/10/bin'

评论列表