Autotools – 自动编译链
简介
虽然现在cmake,qmake 或者 ide的功能越来越强大,也越来越简洁。但是在日常使用中,庞大的开源库,autoconfig 还是占据大半壁江山。
笔者的打包工作历程,也是从简单的可执行文件的拷贝到生成安装包,也是走全了。但是实际工作一直也没有深究过编译语法,打包工具的编写。到前一段时间,自己梳理了常用的编译,安装语法等。也了解了一些常用工具,cmake,qmake的打包语法,以及 checkinstall。今天又研究了一下 autotools.
工具包
安装
代码语言:javascript复制apt install autoconf automake libtool
- 命令工具
autoscan
aclocal
autoheader
automake
autoconf
Demo
代码结构
代码语言:javascript复制.
└── server
├── Makefile
├── bin
│ ├── 20201011.log
│ ├── 20201015.log
│ └── test.ini
└── src
├── Mutex.cpp
├── Mutex.h
├── MyDataBase.cpp
├── MyDataBase.h
├── VxDType.h
├── VxNtpHelper.cpp
├── VxNtpHelper.h
├── epoll_socket.cpp
├── epoll_socket.h
├── ini.cpp
├── ini.h
├── log.cpp
├── log.h
├── main.cpp
Makefile 是 之前编译需要简单的写的一个Makefile 。先将Makefile 删除。
Makefile.am编写
从Demo的文件目录看,为双层目录,server目录即常说的 top-lever ,顶层目录。第二层目录为,bin之前的输出目录也为配置文件目录,src为源码目录。Makefile.am
Makefile.am 文件是什么, Makefile.am->Makefile.in->Makefile,autoMake 工具利用 Makefile.am 最终生成Makefile 文件,进行最终的编译。
Makefile.am 文件,在 需要编译的目录或者编译以来目录每个目录都需要添加对应的Makefile.am 文件。
下边为笔者简单的说明:
Makefile.am 的 宏定义语法规则为:
文件类型 | 书写格式 |
---|---|
可执行文件 | bin_PROGRAMS = main main_SOURCES = main.cppmain_LDADD = (静态库) main_LDFLAGS=(动态库)main_DEPENENCIES= |
静态库 | lib_LIBRARIES= libmain.amain_SOURCES = main.cppmain_LDADD = main_LDFLAGSmain_DEPENENCIES= |
头文件 | include_HEADERS = config.h |
数据文件 | data_DATA= data1 data2 |
基本相对路径:
路径变量 | 含义 |
---|---|
$(top_srcdir) | 工程最顶层目录,用于引用源程序 |
$(top_builddor) | 定义了生成目标文件上最上层目录,用于引用.o 等编译出来的目标文件 |
详细说明请看博文:Makefile.am
下述是笔者编写的Makefile
- 第一层目录的Makefile
SUBDIRS=src
- 第二层目录的Makefile(src)
bin_PROGRAMS=gameserver
gameserver_SOURCES=main.cpp Mutex.cpp Mutex.h MyDataBase.cpp MyDataBase.h
VxDType.h VxNtpHelper.cpp VxNtpHelper.h epoll_socket.cpp epoll_socket.h
ini.cpp ini.h log.cpp log.h
msg_handle.cpp msg_handle.h openssl_ras.cpp openssl_ras.h
protocol.h queuedata.h systeminfo.cpp systeminfo.h
thread_py.cpp thread_py.h time_check.cpp time_check.h tinyjson.hpp
gameserver_LDFLAGS= -lpthread -lcrypto -lmysqlclient
autoscan
下述时间简介
代码语言:javascript复制Usage: /usr/bin/autoscan [OPTION]... [SRCDIR]
Examine source files in the directory tree rooted at SRCDIR, or the
current directory if none is given. Search the source files for
common portability problems, check for incompleteness of
`configure.ac', and create a file `configure.scan' which is a
preliminary `configure.ac' for that package.
-h, --help print this help, then exit
-V, --version print version number, then exit
-v, --verbose verbosely report processing
-d, --debug don't remove temporary files
Library directories:
-B, --prepend-include=DIR prepend directory DIR to search path
-I, --include=DIR append directory DIR to search path
autoscan。用于扫描当前目录,或者说遍历当前目录以及所有子目录文件。会生成两个文件一个 configure.log 和 configure.scan
代码语言:javascript复制mv configure.scan configure.ac // 将 configure.scan 重命名为 configure.ac
vim configutr.ac //修改configure.ac 的内容
笔者的内容如下:
代码语言:javascript复制# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69]) #autoconf 版本
AC_INIT(gameserver, 1.0, 779508400@qq.com) # 工程信息 (目标文件名, 版本信息,联系信息)
AM_INIT_AUTOMAKE(gameserver, 1.0) #添加 AM_INIT_AUTOMAKE(目标文件名, 版本信息) 必须需要
AC_CONFIG_SRCDIR([src/main.cpp]) # 生成可执行文件的cpp 文件
AC_CONFIG_HEADERS([config.h]) # 项目配置的 .h 文件
# Checks for programs.
AC_PROG_CXX # 检测C 编译器
AC_PROG_CC #检测c编译器
# Checks for libraries.
# FIXME: Replace `main' with a function in `-lcrypto':
AC_CHECK_LIB([crypto], [main])
# FIXME: Replace `main' with a function in `-lmysqlclient':
AC_CHECK_LIB([mysqlclient], [main])
# FIXME: Replace `main' with a function in `-lpthread':
AC_CHECK_LIB([pthread], [main])
# 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 sys/statfs.h sys/time.h unistd.h])
# Checks for typedefs, structures, and compiler characteristics.
AC_CHECK_HEADER_STDBOOL
AC_C_INLINE
AC_TYPE_SIZE_T
AC_TYPE_UINT32_T
# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_MKTIME
AC_CHECK_FUNCS([gettimeofday inet_ntoa localtime_r memset regcomp socket strerror])
AC_CONFIG_FILES([Makefile
src/Makefile])
AC_OUTPUT
aclocal
看一句官方说明:
代码语言:javascript复制Generate 'aclocal.m4' by scanning 'configure.ac' or 'configure.in'
根据扫描文件 **configure.ac ** 或者 configure.in 生成 aclocal.m4 文件
autoheader
下述是官方说明,最终生成 config.h.in
代码语言:javascript复制Create a template file of C `#define' statements for `configure' to
use. To this end, scan TEMPLATE-FILE, or `configure.ac' if present,
or else `configure.in'.
automake
这次的执行命令是 automake --add-missing
’ ,其目的是,生成 Makefile.in 并且 添加缺少 生成 包 所缺少的依赖文件。
Generate Makefile.in for configure from Makefile.am.
-a, --add-missing add missing standard files to package
其他一些相关配置,个人认为相对比较重要的,根据自己的实际去配置。
代码语言:javascript复制Library files:
-a, --add-missing add missing standard files to package
--libdir=DIR set directory storing library files
--print-libdir print directory storing library files
-c, --copy with -a, copy missing files (default is symlink)
-f, --force-missing force update of standard files
autoconf
生成最终的 configure 文件。
官方解释
代码语言:javascript复制Generate a configuration script from a TEMPLATE-FILE if given, or
`configure.ac' if present, or else `configure.in'. Output is sent
to the standard output if TEMPLATE-FILE is given, else into
`configure'.
到这一步,已经完成了一个简单的Demol 。
其他
查看一下 configure 的一些信息,看看长什么样
代码语言:javascript复制 ./configure --help
`configure' configures gameserver 1.0 to adapt to many kinds of systems.
Usage: ./configure [OPTION]... [VAR=VALUE]...
To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE. See below for descriptions of some of the useful variables.
Defaults for the options are specified in brackets.
Configuration:
-h, --help display this help and exit
--help=short display options specific to this package
--help=recursive display the short help of all the included packages
-V, --version display version information and exit
-q, --quiet, --silent do not print `checking ...' messages
--cache-file=FILE cache test results in FILE [disabled]
-C, --config-cache alias for `--cache-file=config.cache'
-n, --no-create do not create output files
--srcdir=DIR find the sources in DIR [configure dir or `..']
Installation directories:
--prefix=PREFIX install architecture-independent files in PREFIX
[/usr/local]
--exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
[PREFIX]
By default, `make install' will install all the files in
`/usr/local/bin', `/usr/local/lib' etc. You can specify
an installation prefix other than `/usr/local' using `--prefix',
for instance `--prefix=$HOME'.
For better control, use the options below.
Fine tuning of the installation directories:
--bindir=DIR user executables [EPREFIX/bin]
--sbindir=DIR system admin executables [EPREFIX/sbin]
--libexecdir=DIR program executables [EPREFIX/libexec]
--sysconfdir=DIR read-only single-machine data [PREFIX/etc]
--sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com]
--localstatedir=DIR modifiable single-machine data [PREFIX/var]
--runstatedir=DIR modifiable per-process data [LOCALSTATEDIR/run]
--libdir=DIR object code libraries [EPREFIX/lib]
--includedir=DIR C header files [PREFIX/include]
--oldincludedir=DIR C header files for non-gcc [/usr/include]
--datarootdir=DIR read-only arch.-independent data root [PREFIX/share]
--datadir=DIR read-only architecture-independent data [DATAROOTDIR]
--infodir=DIR info documentation [DATAROOTDIR/info]
--localedir=DIR locale-dependent data [DATAROOTDIR/locale]
--mandir=DIR man documentation [DATAROOTDIR/man]
--docdir=DIR documentation root [DATAROOTDIR/doc/gameserver]
--htmldir=DIR html documentation [DOCDIR]
--dvidir=DIR dvi documentation [DOCDIR]
--pdfdir=DIR pdf documentation [DOCDIR]
--psdir=DIR ps documentation [DOCDIR]
Program names:
--program-prefix=PREFIX prepend PREFIX to installed program names
--program-suffix=SUFFIX append SUFFIX to installed program names
--program-transform-name=PROGRAM run sed PROGRAM on installed program names
Optional Features:
--disable-option-checking ignore unrecognized --enable/--with options
--disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no)
--enable-FEATURE[=ARG] include FEATURE [ARG=yes]
--enable-silent-rules less verbose build output (undo: "make V=1")
--disable-silent-rules verbose build output (undo: "make V=0")
--enable-dependency-tracking
do not reject slow dependency extractors
--disable-dependency-tracking
speeds up one-time build
Some influential environment variables:
CXX C compiler command
CXXFLAGS C compiler flags
LDFLAGS linker flags, e.g. -L<lib dir> if you have libraries in a
nonstandard directory <lib dir>
LIBS libraries to pass to the linker, e.g. -l<library>
CPPFLAGS (Objective) C/C preprocessor flags, e.g. -I<include dir> if
you have headers in a nonstandard directory <include dir>
CC C compiler command
CFLAGS C compiler flags
CPP C preprocessor
Use these variables to override the choices made by `configure' or to help
it to find libraries and programs with nonstandard names/locations.
Report bugs to <779508400@qq.com>.
基本安装 安装,安装重定向都有了。还算属于项目标准规范。
代码语言:javascript复制../configure
make
make install
make dist
make distclean
make distcheck
到此一个简单的 autotools 标准工程模板就完成了。 其他的静态工程,动态工程,多工程目录也类似。根据自己需求创建就可以了。
算是有一个简单的只是get了。
附件
autotools的详细使用
Linux下autoTools工具集使用介绍
autotools使用
GNU构建系统和AutoTools
Makefile.am编写规则