使用autotool管理源代码

libwp是我编写的一个函数库,封装了标准C、Unix、Pthread、BSD socket里面一些常用的函数,主要用于错误处理。

源代码放在/libwpu/src下,包括以下文件:

lyre@linux-4179e1:~/libwpu/src> ls *.c *.h
libwp.h   wpbase.h     wppthread.h  wpsocket.h  wpstdc.h  wpunix.h
wpbase.c  wppthread.c  wpsocket.c   wpstdc.c    wpunix.c  wpunix_rio.c

我们需要编辑以下几个文件:

/libwpu/configure.ac
/libwpu/Makefile.am
/libwpu/src/Makefile.am

1、/libwpu/configure.ac

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.65)
AC_INIT([libwp],[0.0.1],[4179e1@gmail.com])
AC_CONFIG_SRCDIR([src/wpbase.c])
AC_CONFIG_HEADERS([src/config.h])
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_AUX_DIR([build-aux])
AM_INIT_AUTOMAKE([gnu -Wall -Werror])
LT_INIT

RELEASE="1"
AC_SUBST(RELEASE)

# Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AC_LANG([C])

# Checks for libraries.

# Checks for header files.
AC_CHECK_HEADERS([arpa/inet.h fcntl.h limits.h netdb.h shadow.h stdlib.h string.h sys/ioctl.h sys/socket.h sys/time.h syslog.h unistd.h utime.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
AC_TYPE_UID_T
AC_TYPE_MODE_T
AC_TYPE_OFF_T
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_TYPE_SSIZE_T
AC_TYPE_UINT32_T

# Checks for library functions.
AC_FUNC_CHOWN
AC_FUNC_FORK
AC_FUNC_FSEEKO
AC_FUNC_GETGROUPS
AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK
AC_FUNC_MALLOC
AC_FUNC_MKTIME
AC_FUNC_MMAP
AC_FUNC_REALLOC
AC_CHECK_FUNCS([atexit dup2 fchdir fdatasync ftruncate getcwd gethostname getspnam gettimeofday lchown mkdir mkfifo munmap pathconf putenv rmdir setenv socket strerror uname utime])

AC_CONFIG_FILES([libwp.spec Makefile src/Makefile])
AC_OUTPUT

这个文件也可以由autoscan生成,但是需要手工修改其内容,并mv configure.scan configure.ac

2、/libwpu/Makefile.am

ACLOCAL_AMFLAGS = -I m4
SUBDIRS = src

3、/libwpu/src/Makefile.am

AM_CFLAGS = -Wall

ACLOCAL_AMFLAGS = -I m4

lib_LTLIBRARIES = libwp.la

libwp_la_SOURCES = wpbase.c wppthread.c wpsocket.c wpstdc.c wpunix.c wpunix_rio.c libwp.h wpbase.h wppthread.h wpsocket.h wpstdc.h wpunix.h

include_HEADERS = libwp.h wpbase.h wppthread.h wpsocket.h wpstdc.h wpunix.h

接下来执行

1、建立m4和build-aux目录

lyre@linux-4179e1:~/libwpu>mkdir m4 build-aux

2、把m4宏复制到m4目录下,其中列出了复制文件的列表

lyre@linux-4179e1:~/libwpu> aclocal -I m4 --install
aclocal: installing `m4/libtool.m4' from `/usr/share/aclocal/libtool.m4'
aclocal: installing `m4/ltoptions.m4' from `/usr/share/aclocal/ltoptions.m4'
aclocal: installing `m4/ltsugar.m4' from `/usr/share/aclocal/ltsugar.m4'
aclocal: installing `m4/ltversion.m4' from `/usr/share/aclocal/ltversion.m4'
aclocal: installing `m4/lt~obsolete.m4' from `/usr/share/aclocal/lt~obsolete.m4'

3、生成config.h.in

lyre@linux-4179e1:~/libwpu> autoheader
lyre@linux-4179e1:~/libwpu> ls -l src/config.h.in
-rw-r--r-- 1 lyre users 6912 11-30 16:45 src/config.h.in

4、复制libtool的脚本

lyre@linux-4179e1:~/libwpu> libtoolize -fc
libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, `build-aux'.
libtoolize: copying file `build-aux/ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIR, `m4'.
libtoolize: copying file `m4/libtool.m4'
libtoolize: copying file `m4/ltoptions.m4'
libtoolize: copying file `m4/ltsugar.m4'
libtoolize: copying file `m4/ltversion.m4'
libtoolize: copying file `m4/lt~obsolete.m4

5、生成configure脚本

lyre@linux-4179e1:~/libwpu> autoconf

6、执行automake

lyre@linux-4179e1:~/libwpu> automake --add-missing -fc
configure.ac:11: installing `build-aux/config.guess'
configure.ac:11: installing `build-aux/config.sub'
configure.ac:10: installing `build-aux/install-sh'
configure.ac:10: installing `build-aux/missing'
src/Makefile.am: installing `build-aux/depcomp'
Makefile.am: installing `./INSTALL'
Makefile.am: required file `./NEWS' not found
Makefile.am: required file `./README' not found
Makefile.am: required file `./AUTHORS' not found
Makefile.am: required file `./ChangeLog' not found
Makefile.am: installing `./COPYING' using GNU General Public License v3 file
Makefile.am:     Consider adding the COPYING file to the version control system
Makefile.am:     for your code, to avoid questions about which license your project uses.

7、由于我采用了gnu的style,automake给了一些警告,说缺少几个文件,手动创建即可

touch NEWS README AUTHORS ChangeLog

8、到此为止,autotool的编译环境就搭建好了,测试一下:

lyre@linux-4179e1:~/libwpu> mkdir build     
lyre@linux-4179e1:~/libwpu> cd build
lyre@linux-4179e1:~/libwpu/build> ../configure && make

9、如果没有问题,要发布代码之前最好用make distcheck检查一遍

lyre@linux-4179e1:~/libwpu> make distcheck

看到这样的结果,应该就没问题了

=============================================
libwp-0.0.1 archives ready for distribution:
libwp-0.0.1.tar.gz

updatedupdated2022-02-222022-02-22