使用autotools小结

一、autotools使用步骤

autoscan #扫面当前目录、源文件,生成configure.scan文件,重命名为configure.ac
Makefile.am#手动创建Makefile.am并编辑
libtoolize -f -c# 注意:仅仅在生成动态库时使用
aclocal #根据configure.in生成aclocal.m4文件以及autom4te.cache文件夹
autoheader #生成配置头文件的模板config.h.in
touch README NEWS AUTHORS ChangeLog #生成一些声明性文件
autoconf #根据configure.in和aclocal.m4来产生configure文件
automake --add-missing #生成Makefiel.in和所需要的脚本
./configure #通过--prefix=设置安装目录
make
make install 安装 将编译成功的可执行文件安装到系统目录中,一般为/usr/local/bin目录
make uninstall #卸载
make clean#清除上次的make命令所产生的object文件(后缀为“.o”的文件)及可执行文件
make dist #制作发布的软件包
make distcheck #生成发布软件包并对其进行测试检查,以确定发布包的正确性。这个操作将自动把压缩包文件解开,然后执行configure命令,并且执行make,来确认编译不出现错误。
make distclean#类似make clean,但同时也将configure生成的文件全部删除掉,包括Makefile文件

备注:

 修改了configure.ac,需程序执行aclocal、autoconf、automake --add-missing
 修改了Makefile.am,需程序执行automake --add-missing

二、编译可执行程序

三、编译静态库

四、编译动态库

进入代码目录
$ autoscan
$ mv configure.scan configure.ac
$ vim configure.ac
编辑configure.ac
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.69])
AC_INIT(xxx.so, 1.0.0, email address) 
AC_CONFIG_SRCDIR([xxx.h])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE([subdir-objects])	### 新增

AC_CONFIG_MACRO_DIR([m4])  ### 新增,根据 libtoolize -f -c 执行结果提示新增

# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
AC_PROG_LIBTOOL  ### 新增,生成动态库必须

# Checks for libraries.
# FIXME: Replace `main' with a function in `-lcrypto':
AC_CHECK_LIB([crypto], [main])
# FIXME: Replace `main' with a function in `-lssl':
AC_CHECK_LIB([ssl], [main])
# FIXME: Replace `main' with a function in `-lz':
AC_CHECK_LIB([z], [main])

# Checks for header files.
AC_CHECK_HEADERS([arpa/inet.h fcntl.h float.h inttypes.h limits.h locale.h memory.h netdb.h netinet/in.h stddef.h stdint.h stdlib.h string.h strings.h sys/ioctl.h sys/socket.h sys/time.h sys/timeb.h unistd.h])

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

# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_MKTIME
AC_FUNC_STRERROR_R
AC_FUNC_STRTOD
AC_CHECK_FUNCS([ftime gethostbyname gettimeofday localtime_r memset select socket strchr strerror strstr])

AC_CONFIG_FILES([Makefile])
AC_OUTPUT
编写Makefile.am
Makefile.am
AUTOMAKE_OPTIONS = foreign
lib_LTLIBRARIES = xxx.la
xxx_la_SOURCES =
xxx_la_LIBADD =   -lcrypto -lssl -lz
xxx_la_DEPENDENCIES	=
xxx_la_LDFLAGS= -shared -fPIC -version-number 1:0:0	### 动态库编译完成 libtool 自动对对库创建软链接; 执行make install时,libtool 自动对对库创建软链接
xxx_la_CFLAGS =  -Wall -O2  -pedantic -Wextra  -DWITH_OPENSSL -DWITH_DOM -DWITH_NO_C_LOCALE -I./ 
xxx_la_CPPFLAGS = -Wall -O2 -std=c++11 -pedantic -Wextra -DWITH_OPENSSL -DWITH_DOM -DWITH_NO_C_LOCALE -I./

ACLOCAL_AMFLAGS=-I m4 		 ### 根据 libtoolize -f -c 执行结果提示新增

libtoolize
$ libtoolize -f -c   
## 执行后,会提示在Makefile.am 和  configure.ac中是否需要添加内容,如提示需手动添加
$ aclocal
$ autoheader
$ touch README NEWS AUTHORS ChangeLog
$ autoconf
$ automake --add-missing
### 执行后不提示报错,即可进行./configure && make 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章