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

相关文章

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

基于Python构建一个高效词汇表

《基于Python构建一个高效词汇表》在自然语言处理(NLP)领域,构建高效的词汇表是文本预处理的关键步骤,本文将解析一个使用Python实现的n-gram词频统计工具,感兴趣的可以了解下... 目录一、项目背景与目标1.1 技术需求1.2 核心技术栈二、核心代码解析2.1 数据处理函数2.2 数据处理流程

Python FastMCP构建MCP服务端与客户端的详细步骤

《PythonFastMCP构建MCP服务端与客户端的详细步骤》MCP(Multi-ClientProtocol)是一种用于构建可扩展服务的通信协议框架,本文将使用FastMCP搭建一个支持St... 目录简介环境准备服务端实现(server.py)客户端实现(client.py)运行效果扩展方向常见问题结

详解如何使用Python构建从数据到文档的自动化工作流

《详解如何使用Python构建从数据到文档的自动化工作流》这篇文章将通过真实工作场景拆解,为大家展示如何用Python构建自动化工作流,让工具代替人力完成这些数字苦力活,感兴趣的小伙伴可以跟随小编一起... 目录一、Excel处理:从数据搬运工到智能分析师二、PDF处理:文档工厂的智能生产线三、邮件自动化:

详解如何使用Python从零开始构建文本统计模型

《详解如何使用Python从零开始构建文本统计模型》在自然语言处理领域,词汇表构建是文本预处理的关键环节,本文通过Python代码实践,演示如何从原始文本中提取多尺度特征,并通过动态调整机制构建更精确... 目录一、项目背景与核心思想二、核心代码解析1. 数据加载与预处理2. 多尺度字符统计3. 统计结果可