Autotools -- 自动编译链工程构建

2024-06-01 22:48

本文主要是介绍Autotools -- 自动编译链工程构建,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Autotools – 自动编译链

简介

虽然现在cmake,qmake 或者 ide的功能越来越强大,也越来越简洁。但是在日常使用中,庞大的开源库,autoconfig 还是占据大半壁江山。

笔者的打包工作历程,也是从简单的可执行文件的拷贝到生成安装包,也是走全了。但是实际工作一直也没有深究过编译语法,打包工具的编写。到前一段时间,自己梳理了常用的编译,安装语法等。也了解了一些常用工具,cmake,qmake的打包语法,以及 checkinstall。今天又研究了一下 autotools.

工具包

安装

apt install autoconf automake libtool
  • 命令工具
autoscan
aclocal
autoheader
automake
autoconf

Demo

代码结构

.
└── 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.cpp
main_LDADD = (静态库)
main_LDFLAGS=(动态库)
main_DEPENENCIES=
静态库lib_LIBRARIES= libmain.a
main_SOURCES = main.cpp
main_LDADD =
main_LDFLAGS
main_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

下述时间简介

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 filesLibrary directories:-B, --prepend-include=DIR  prepend directory DIR to search path-I, --include=DIR          append directory DIR to search path

autoscan。用于扫描当前目录,或者说遍历当前目录以及所有子目录文件。会生成两个文件一个 configure.logconfigure.scan

mv configure.scan configure.ac // 将 configure.scan 重命名为 configure.ac
vim configutr.ac //修改configure.ac 的内容

笔者的内容如下:

#                                               -*- 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([Makefilesrc/Makefile])
AC_OUTPUT

aclocal

看一句官方说明:

Generate 'aclocal.m4' by scanning 'configure.ac' or 'configure.in'

根据扫描文件 **configure.ac ** 或者 configure.in 生成 aclocal.m4 文件

autoheader

下述是官方说明,最终生成 config.h.in

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

其他一些相关配置,个人认为相对比较重要的,根据自己的实际去配置。

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 文件。

官方解释

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 的一些信息,看看长什么样

 ./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 namesOptional 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-trackingdo not reject slow dependency extractors--disable-dependency-trackingspeeds up one-time buildSome influential environment variables:CXX         C++ compiler commandCXXFLAGS    C++ compiler flagsLDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in anonstandard 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> ifyou have headers in a nonstandard directory <include dir>CC          C compiler commandCFLAGS      C compiler flagsCPP         C preprocessorUse 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>.

基本安装 安装,安装重定向都有了。还算属于项目标准规范。

../configure
make
make install
make dist 
make distclean
make distcheck

到此一个简单的 autotools 标准工程模板就完成了。 其他的静态工程,动态工程,多工程目录也类似。根据自己需求创建就可以了。

算是有一个简单的只是get了。

附件

autotools的详细使用

Linux下autoTools工具集使用介绍

autotools使用

GNU构建系统和AutoTools

Makefile.am编写规则

这篇关于Autotools -- 自动编译链工程构建的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1022319

相关文章

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Go Mongox轻松实现MongoDB的时间字段自动填充

《GoMongox轻松实现MongoDB的时间字段自动填充》这篇文章主要为大家详细介绍了Go语言如何使用mongox库,在插入和更新数据时自动填充时间字段,从而提升开发效率并减少重复代码,需要的可以... 目录前言时间字段填充规则Mongox 的安装使用 Mongox 进行插入操作使用 Mongox 进行更

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

nginx-rtmp-module构建流媒体直播服务器实战指南

《nginx-rtmp-module构建流媒体直播服务器实战指南》本文主要介绍了nginx-rtmp-module构建流媒体直播服务器实战指南,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录1. RTMP协议介绍与应用RTMP协议的原理RTMP协议的应用RTMP与现代流媒体技术的关系2

IDEA如何让控制台自动换行

《IDEA如何让控制台自动换行》本文介绍了如何在IDEA中设置控制台自动换行,具体步骤为:File-Settings-Editor-General-Console,然后勾选Usesoftwrapsin... 目录IDEA如何让控制台自http://www.chinasem.cn动换行操作流http://www

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

vscode保存代码时自动eslint格式化图文教程

《vscode保存代码时自动eslint格式化图文教程》:本文主要介绍vscode保存代码时自动eslint格式化的相关资料,包括打开设置文件并复制特定内容,文中通过代码介绍的非常详细,需要的朋友... 目录1、点击设置2、选择远程--->点击右上角打开设置3、会弹出settings.json文件,将以下内

Python脚本实现自动删除C盘临时文件夹

《Python脚本实现自动删除C盘临时文件夹》在日常使用电脑的过程中,临时文件夹往往会积累大量的无用数据,占用宝贵的磁盘空间,下面我们就来看看Python如何通过脚本实现自动删除C盘临时文件夹吧... 目录一、准备工作二、python脚本编写三、脚本解析四、运行脚本五、案例演示六、注意事项七、总结在日常使用

SpringBoot项目启动后自动加载系统配置的多种实现方式

《SpringBoot项目启动后自动加载系统配置的多种实现方式》:本文主要介绍SpringBoot项目启动后自动加载系统配置的多种实现方式,并通过代码示例讲解的非常详细,对大家的学习或工作有一定的... 目录1. 使用 CommandLineRunner实现方式:2. 使用 ApplicationRunne

Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单

《Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单》:本文主要介绍Springboot的ThreadPoolTaskScheduler线... 目录ThreadPoolTaskScheduler线程池实现15分钟不操作自动取消订单概要1,创建订单后