一、生成configure过程中各文件之间的关系图

二、详细介绍

autoscan: 扫描源代码以搜寻普通的可移植性问题,比如检查编译器,库,头文件等,生成文件configure.scan,它是configure.ac的一个雏形。

aclocal:根据已经安装的宏,用户定义宏和acinclude.m4文件中的宏将configure.ac文件所需要的宏集中定义到文件 aclocal.m4中。aclocal是一个perl 脚本程序,它的定义是:“aclocal - create aclocal.m4 by scanning configure.ac”

automake:将Makefile.am中定义的结构建立Makefile.in,然后configure脚本将生成的Makefile.in文件转换 为Makefile。如果在configure.ac中定义了一些特殊的宏,比如AC_PROG_LIBTOOL,它会调用libtoolize,否则它 会自己产生config.guess和config.sub

autoconf:将configure.ac中的宏展开,生成configure脚本。这个过程可能要用到aclocal.m4中定义的宏。

三、实例

1.测试代码(定义两个文件hello.h和hello.c)

/*hello.c*/ #include <iostream>
#include "hello.h" using namespace std; int main()
{
CHello a;
return 0;
}
/*hello.h*/ #ifndef __HELLO_H__
#define __HELLO_H__ #include<iostream>
using namespace std; class CHello
{
public:
CHello(){ cout<<"Hello!"<<endl;}
~CHello(){ cout<<"Bye!"<<endl;}
}; #endif

2.操作步骤

(1)安装依赖的包

[root@bogon autoconfig]# yum -y install automake autoconf

automake包括:aclocal、automake等

autoconf包括:autoscan、autoconf等

(2)autoscan

[root@bogon autoconfig]# ll
-rw-r--r-- 1 root root 105 Jun 4 hello.cpp
-rw-r--r-- 1 root root 189 Jun 4 hello.h
[root@bogon autoconfig]# autoscan
[root@bogon autoconfig]# ll
total 12
-rw-r--r-- 1 root root 0 Jun 4 autoscan.log
-rw-r--r-- 1 root root 481 Jun 4 configure.scan
-rw-r--r-- 1 root root 105 Jun 4 hello.cpp
-rw-r--r-- 1 root root 189 Jun 4 hello.h

(3)aclocal

[root@bogon autoconfig]# mv configure.scan configure.ac
[root@bogon autoconfig]# vim configure.ac /*将下面红色加粗的部分修改掉*/
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT(hello, 1.0, admin@163.com) AM_INIT_AUTOMAKE(hello, 1.0)
AC_CONFIG_SRCDIR([hello.cpp])
AC_CONFIG_HEADERS([config.h])
# Checks for programs.
AC_PROG_CXX
AC_PROG_CC # Checks for libraries.
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_OUTPUT(Makefile)
[root@bogon autoconfig]# aclocal
[root@bogon autoconfig]# ll
total 52
-rw-r--r-- 1 root root 37794 Jun 4 aclocal.m4
drwxr-xr-x 2 root root 51 Jun 4 autom4te.cache
-rw-r--r-- 1 root root 0 Jun 4 autoscan.log
-rw-r--r-- 1 root root 492 Jun 4 configure.ac
-rw-r--r-- 1 root root 105 Jun 4 hello.cpp
-rw-r--r-- 1 root root 189 Jun 4 hello.h

下面给出本文件的简要说明(所有以”#”号开始的行为注释): · AC_PREREQ 宏声明本文件要求的autoconf版本,本例使用的版本为2.59。 · AC_INIT 宏用来定义软件的名称和版本等信息,”FULL-PACKAGE-NAME”为软件包名称,”VERSION”为软件版本号,”BUG-REPORT-ADDRESS”为BUG报告地址(一般为软件作者邮件地址)。 ·AC_CONFIG_SRCDIR 宏用来侦测所指定的源码文件是否存在,来确定源码目录的有效性。此处为当前目录下的hello.c。 ·AC_CONFIG_HEADER 宏用于生成config.h文件,以便autoheader使用。 ·AC_PROG_CC 用来指定编译器,如果不指定,选用默认gcc。 ·AC_OUTPUT 用来设定 configure 所要产生的文件,如果是makefile,configure会把它检查出来的结果带入makefile.in文件产生合适的makefile。使用Automake时,还需要一些其他的参数,这些额外的宏用aclocal工具产生。

(3)autoconf

[root@bogon autoconfig]# autoconf
[root@bogon autoconfig]# ll
total 204
-rw-r--r-- 1 root root 37794 Jun 4 aclocal.m4
drwxr-xr-x 2 root root 81 Jun 4 autom4te.cache
-rw-r--r-- 1 root root 0 Jun 4 autoscan.log
-rwxr-xr-x 1 root root 154727 Jun 4 configure
-rw-r--r-- 1 root root 492 Jun 4 configure.ac
-rw-r--r-- 1 root root 105 Jun 4 hello.cpp
-rw-r--r-- 1 root root 189 Jun 4 hello.h

此时可以看到已经生成了configure

(4)autoheader

[root@bogon autoconfig]# autoheader
[root@bogon autoconfig]# ll
total 208
-rw-r--r-- 1 root root 37794 Jun 4 aclocal.m4
drwxr-xr-x 2 root root 81 Jun 4 autom4te.cache
-rw-r--r-- 1 root root 0 Jun 4 autoscan.log
-rw-r--r-- 1 root root 625 Jun 4 config.h.in
-rwxr-xr-x 1 root root 154727 Jun 4 configure
-rw-r--r-- 1 root root 492 Jun 4 configure.ac
-rw-r--r-- 1 root root 105 Jun 4 hello.cpp
-rw-r--r-- 1 root root 189 Jun 4 hello.h

autoheader生成了configure.h.in如果在configure.ac中定义了AC_CONFIG_HEADER,那么此文件就需要;

(5)Makefile.am

[root@bogon autoconfig]# vim Makefile.am
[root@bogon autoconfig]# cat Makefile.am
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=hello
hello_SOURCES=hello.cpp hello.h

· AUTOMAKE_OPTIONS 为设置Automake的选项。由于GNU对自己发布的软件有严格的规范,比如必须附带许可证声明文件COPYING等,否则Automake执行时会报错。Automake提供了3种软件等级:foreign、gnu和gnits,供用户选择,默认等级为gnu。本例使需用foreign等级,它只检测必须的文件。 · bin_PROGRAMS 定义要产生的执行文件名。如果要产生多个执行文件,每个文件名用空格隔开。 · hello_SOURCES 定义”hello”这个执行程序所需要的原始文件。如果”hello”这个程序是由多个原始文件所产生的,则必须把它所用到的所有原始文件都列出来,并用空格隔开。例如:若目标体”hello”需要”hello.c”、”hello.h”两个依赖文件,则定义hello_SOURCES=hello.c hello.h。

(6)automake

[root@bogon autoconfig]# automake --add-missing
configure.ac:6: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated. For more info, see:
configure.ac:6: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
[root@bogon autoconfig]# ll
total 236
-rw-r--r-- 1 root root 37794 Jun 4 aclocal.m4
drwxr-xr-x 2 root root 81 Jun 4 autom4te.cache
-rw-r--r-- 1 root root 0 Jun 4 autoscan.log
-rw-r--r-- 1 root root 625 Jun 4 config.h.in
-rwxr-xr-x 1 root root 154727 Jun 4 configure
-rw-r--r-- 1 root root 492 Jun 4 configure.ac
-rw-r--r-- 1 root root 105 Jun 4 hello.cpp
-rw-r--r-- 1 root root 189 Jun 4 hello.h
lrwxrwxrwx 1 root root 35 Jun 4 install-sh -> /usr/share/automake-1.13/install-sh
-rw-r--r-- 1 root root 79 Jun 4 Makefile.am
-rw-r--r-- 1 root root 22227 Jun 4 Makefile.in
lrwxrwxrwx 1 root root 32 Jun 4 missing -> /usr/share/automake-1.13/missing

此步主要是为了生成Makefile.in,加上--add-missing参数后,会补全缺少的脚本;

(6)测试

[root@bogon autoconfig]# ./configure
[root@bogon autoconfig]# make
[root@bogon autoconfig]# ./hello
Hello!
Bye!

和平时安装许多开源软件一样操作

(7)打包

[root@bogon autoconfig]# make dist
[root@bogon autoconfig]# ll
total 436
-rw-r--r-- 1 root root 37794 Jun 4 aclocal.m4
drwxr-xr-x 2 root root 81 Jun 4 autom4te.cache
-rw-r--r-- 1 root root 0 Jun 4 autoscan.log
-rw-r--r-- 1 root root 758 Jun 4 config.h
-rw-r--r-- 1 root root 625 Jun 4 config.h.in
-rw-r--r-- 1 root root 11031 Jun 4 config.log
-rwxr-xr-x 1 root root 32557 Jun 4 config.status
-rwxr-xr-x 1 root root 154727 Jun 4 configure
-rw-r--r-- 1 root root 492 Jun 4 configure.ac
lrwxrwxrwx 1 root root 32 Jun 4 depcomp -> /usr/share/automake-1.13/depcomp
-rwxr-xr-x 1 root root 22250 Jun 4 hello
-rw-r--r-- 1 root root 72021 Jun 4 hello-1.0.tar.gz
-rw-r--r-- 1 root root 105 Jun 4 hello.cpp
-rw-r--r-- 1 root root 189 Jun 4 hello.h
-rw-r--r-- 1 root root 26008 Jun 4 hello.o
lrwxrwxrwx 1 root root 35 Jun 4 install-sh -> /usr/share/automake-1.13/install-sh
-rw-r--r-- 1 root root 23564 Jun 4 Makefile
-rw-r--r-- 1 root root 79 Jun 4 Makefile.am
-rw-r--r-- 1 root root 23869 Jun 4 Makefile.in
lrwxrwxrwx 1 root root 32 Jun 4 missing -> /usr/share/automake-1.13/missing
-rw-r--r-- 1 root root 23 Jun 4 stamp-h1

如果细心的话可以发现,人群中已经出现了hello-1.0.tar.gz就是将已经编译好的文件进行了打包。

  make all:产生我们设定的目标,即此范例中的可执行文件。只打make也可以,此时会开始编译原始码,然后连结,并且产生可执行文件。

  make clean:清除编译产生的可执行文件及目标文件(object file,*.o)。

  make distclean:除了清除可执行文件和目标文件外,把configure所产生的Makefile也清除掉。

  make install:将程序安装至系统中。如果原始码编译无误,且执行结果正确,便可以把程序安装至系统预设的可执行文件存放路径。如果用bin_PROGRAMS宏的话,程序会被安装至/usr/local/bin这个目录。

  make dist:将程序和相关的档案包装成一个压缩文件以供发布。执行完在目录下会产生一个以PACKAGE-VERSION.tar.gz为名称的文件。 PACKAGE和VERSION这两个变数是根据configure.in文件中AM_INIT_AUTOMAKE(PACKAGE,VERSION)的定义。在此范例中会产生test-1.0.tar.gz的档案。

  make distcheck:和make dist类似,但是加入检查包装后的压缩文件是否正常。这个目标除了把程序和相关文件包装成tar.gz文件外,还会自动把这个压缩文件解开,执行 configure,并且进行make all 的动作,确认编译无误后,会显示这个tar.gz文件可供发布了。这个检查非常有用,检查过关的包,基本上可以给任何一个具备GNU开发环境-的人去重新编译。

参考链接:

https://www.cnblogs.com/tinywan/p/7230039.html

https://www.cnblogs.com/bugutian/p/5560548.html

https://www.ibm.com/support/knowledgecenter/zh/ssw_aix_71/com.ibm.aix.cmds3/make.htm

关于makefile的生成原理以及make相关命令的使用的更多相关文章

  1. ajax原理,验证码生成原理

    什么是ajax AJAX:”Asynchronous JavaScript and XML” 中文意思:异步JavaScript和XML 指一种创建交互式网页应用的网页开发技术.   不是指一种单一的 ...

  2. Makefile自动生成头文件依赖

    前言 Makefile自动生成头文件依赖是很常用的功能,本文的目的是想尽量详细说明其中的原理和过程. Makefile模板 首先给出一个本人在小项目中常用的Makefile模板,支持自动生成头文件依赖 ...

  3. Makefile自动生成

    automake/autoconf入门作为Linux下的程序开发人员,大家一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便.一般情况下,大家都是手工写一个简单Makefile ...

  4. Makefile自动生成:cmake

    http://blog.csdn.net/pipisorry/article/details/51647073 编辑makefile文件CMakeLists.txt,使用cmake命令自动生成make ...

  5. Makefile.am和makefile.in生成Makefile

    Makefile.am和makefile.in生成Makefile 很多时候,我们在网上下载的linux开源软件都会遇到一个问题,就是源码里面没有直接的makefile,但是它有Makefile.am ...

  6. Linux Makefile自动生成--config.h

    Linux Makefile自动生成--config.h http://blog.csdn.net/spch2008/article/details/12510805

  7. phpcms模板生成原理

    phpcms模板生成原理2009-09-25 16:14PHPCMS是功能强大的网站管理系统.以后有时间,俺要将其管理.使用.再开发等等都理顺出来.这一篇短文先介绍PHPCMS的模板机制. 所谓网站模 ...

  8. WiFi广告强推的基本技术原理和一些相关问题

    WiFi推原理(转) 本文地址:http://jb.tongxinmao.com/Article/Detail/id/412 WiFi广告强推的基本技术原理和一些相关问题 WiFi广告推送原理就是利用 ...

  9. makefile自动生成学习

    https://www.cnblogs.com/jrglinux/p/6964169.html 关键是如何写Makefile.am  其他的交给 自动工具完成 添加一个 很好的博客 学习下 https ...

随机推荐

  1. 用C#实现多种方式播放Wav声音

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. java判断时间为上午,中午,下午,晚上,凌晨

    public static void main(String[] args) { Date date = new Date(); SimpleDateFormat df = new SimpleDat ...

  3. 配置tomcat允许跨域访问,cors跨域资源共享

    1.导入cors-filter-1.7.jar,java-property-utils-1.9.jar两个jar包 2.在web.xml里配上: <filter> <filter-n ...

  4. Spring注解之@Import

    /** * Indicates one or more {@link Configuration @Configuration} classes to import. * 表示import 一个或多个 ...

  5. postman系列之批量执行接口测试用例

    postman如何批量执行接口测试用例~其实很简单,但是会给我们的工作带来很多方便~ 比如我们写了几十个测试用例,请求都是同一个服务器IP,一旦服务器IP地址从测试环境搬到线上环境,需要修改所有的服务 ...

  6. spring cloud jwt用户鉴权及服务鉴权

    用户鉴权 客户端请求服务时,根据提交的token获取用户信息,看是否有用户信息及用户信息是否正确 服务鉴权 微服务中,一般有多个服务,服务与服务之间相互调用时,有的服务接口比较敏感,比如资金服务,不允 ...

  7. vue中alert toast confirm loading 公用

    import Vue from 'vue' import { ToastPlugin, AlertPlugin, ConfirmPlugin, LoadingPlugin } from 'vux' / ...

  8. py QScrollArea

    # -*- coding: utf-8 -*-import jsonimport loggingimport sysimport requestsfrom PyQt5 import QtWidgets ...

  9. Java集合list,map,set区别及遍历

    1.1 List.Set.Map基本区别 1.List,Set都是继承Collection接口,Map不是. 2.List:LinkedList.ArrayList.Vector Set :HashS ...

  10. Win10系列:C#应用控件基础1

    Button控件 在Windows应用商店应用的开发中,Button控件是使用比较频繁的控件之一,当用户单击Button控件时,会触发相应的单击事件并在定义好的事件处理方法中执行指定的功能.下面将介绍 ...