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

相关文章

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

基于Redis自动过期的流处理暂停机制

《基于Redis自动过期的流处理暂停机制》基于Redis自动过期的流处理暂停机制是一种高效、可靠且易于实现的解决方案,防止延时过大的数据影响实时处理自动恢复处理,以避免积压的数据影响实时性,下面就来详... 目录核心思路代码实现1. 初始化Redis连接和键前缀2. 接收数据时检查暂停状态3. 检测到延时过

Python利用PySpark和Kafka实现流处理引擎构建指南

《Python利用PySpark和Kafka实现流处理引擎构建指南》本文将深入解剖基于Python的实时处理黄金组合:Kafka(分布式消息队列)与PySpark(分布式计算引擎)的化学反应,并构建一... 目录引言:数据洪流时代的生存法则第一章 Kafka:数据世界的中央神经系统消息引擎核心设计哲学高吞吐

Springboot项目构建时各种依赖详细介绍与依赖关系说明详解

《Springboot项目构建时各种依赖详细介绍与依赖关系说明详解》SpringBoot通过spring-boot-dependencies统一依赖版本管理,spring-boot-starter-w... 目录一、spring-boot-dependencies1.简介2. 内容概览3.核心内容结构4.

Go语言使用net/http构建一个RESTful API的示例代码

《Go语言使用net/http构建一个RESTfulAPI的示例代码》Go的标准库net/http提供了构建Web服务所需的强大功能,虽然众多第三方框架(如Gin、Echo)已经封装了很多功能,但... 目录引言一、什么是 RESTful API?二、实战目标:用户信息管理 API三、代码实现1. 用户数据

SpringBoot实现RSA+AES自动接口解密的实战指南

《SpringBoot实现RSA+AES自动接口解密的实战指南》在当今数据泄露频发的网络环境中,接口安全已成为开发者不可忽视的核心议题,RSA+AES混合加密方案因其安全性高、性能优越而被广泛采用,本... 目录一、项目依赖与环境准备1.1 Maven依赖配置1.2 密钥生成与配置二、加密工具类实现2.1

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3