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

相关文章

Java中的StringBuilder之如何高效构建字符串

《Java中的StringBuilder之如何高效构建字符串》本文将深入浅出地介绍StringBuilder的使用方法、性能优势以及相关字符串处理技术,结合代码示例帮助读者更好地理解和应用,希望对大家... 目录关键点什么是 StringBuilder?为什么需要 StringBuilder?如何使用 St

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注

pytorch自动求梯度autograd的实现

《pytorch自动求梯度autograd的实现》autograd是一个自动微分引擎,它可以自动计算张量的梯度,本文主要介绍了pytorch自动求梯度autograd的实现,具有一定的参考价值,感兴趣... autograd是pytorch构建神经网络的核心。在 PyTorch 中,结合以下代码例子,当你

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

Python如何自动生成环境依赖包requirements

《Python如何自动生成环境依赖包requirements》:本文主要介绍Python如何自动生成环境依赖包requirements问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录生成当前 python 环境 安装的所有依赖包1、命令2、常见问题只生成当前 项目 的所有依赖包1、

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

idea maven编译报错Java heap space的解决方法

《ideamaven编译报错Javaheapspace的解决方法》这篇文章主要为大家详细介绍了ideamaven编译报错Javaheapspace的相关解决方法,文中的示例代码讲解详细,感兴趣的... 目录1.增加 Maven 编译的堆内存2. 增加 IntelliJ IDEA 的堆内存3. 优化 Mave

微信公众号脚本-获取热搜自动新建草稿并发布文章

《微信公众号脚本-获取热搜自动新建草稿并发布文章》本来想写一个自动化发布微信公众号的小绿书的脚本,但是微信公众号官网没有小绿书的接口,那就写一个获取热搜微信普通文章的脚本吧,:本文主要介绍微信公众... 目录介绍思路前期准备环境要求获取接口token获取热搜获取热搜数据下载热搜图片给图片加上标题文字上传图片

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、