博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Linux下使用automake、autoconf生成configure文件

Posted on 2019-03-29 18:31  bw_0927  阅读(2469)  评论(0)    收藏  举报

 最近遇到一个要编译的源码居然没有configure文件,网上找到一个编译的方法,只有命令,没有说明,适合救急使用。

  1. aclocal

  2. autoconf

  3. autoheader

  4. automake --add-missing

  5. ./configure

  6. make

  7. sudo make install

 

用自动生成Makefile的工具来生成Makefile的过程中,需要用autoscan命令来生成configure.scan文件,然后将它改名字为configure.ac或者configure.in

在生成的过程中会用autoconf命令来处理configure.ac/configure.in文件,生成一个configure的脚本。
生成后的configure文件是一个可以移植的shell脚本,运行的时候它检查编译环境,来决定哪些库是可以用的,所用到的平台有哪些个特征,那些个头文件和库是已经找到的等等, 然后收集到的这些信息,它修改编译标记,生成一个Makefile文件,同时生成一个包含已定义的预处理符号的config.h文件。configure并不需要运行autoconf,所以我们在发布应用程序之前生成这个文件,如此我们就不必有autoconf的软件包了。
configure.in文件里基本的内容就是一系列的m4宏,在运行时根据传递给它们的参数,定义的宏就会扩展为shell的脚本代码段。也可以手工书写shell代码。
不过我们就不说这个了,要想完全的理解configure.in怎样写,其实是要求有一些m4的知识,还要一些 Bourneshell的知识
要这么多知识做基础好难,不过还好的的是,有省事的方法,就是可以找一个已有的configure.in文件,然后以此来修改它以适应你所要完成的任务。如果想了解可以看看autoconf手册,其中介绍了很多预先写好的宏。
 
 

下面我们就来通过一个简单的configure.ac文件,来了解一些非常常用的宏和含义:

AC_INIT(src/hello.c)

AM_CONFIG_HEADER(config.h)

AM_INIT_AUTOMAKE(GnomeHello,0.1)

AM_MAINTAINER_MODE

AM_ACLOCAL_INCLUDE(macros)

GNOME_INIT

AC_PROG_CC

AC_ISC_POSIX

AC_HEADER_STDC

AC_ARG_PROGRAM

AM_PROG_LIBTOOL

GNOME_COMPILE_WARNINGS

ALL_LINGUAS=”es”

AM_GNU_GETTEXT

AC_SUBST(CFLAGS)

AC_SUBST(CPPFLAGS)

AC_SUBST(LDFLAGS)

AC_OUTPUT([

Makefile

macros/Makefile

src/Makefile

intl/Makefile

po/Makefile.in

pixmaps/Makefile

doc/Makefile

doc/C/Makefile

doc/es/Makefile

])

下面我们逐一对例子中的代码进行一下解释,例子中以AC开头的宏来自autoconf,以AM开头的宏来自automake。可以从autoconf或 automake中寻求帮助,这一点很有用

以GNOME开头的宏来自于Gnomemacros目录这些宏都是用m4宏语言写的。如果将 autoconf和automake安装在/usr目录下,autoconf和automake中的标准宏一般放在/usr/share/aclocal 目录下。

AC_INIT总是configure.in中的第一个宏。它扩展为许多可由其他configure脚本共享的模板文件代码。这些代码解析传到 configure中的命令行参数。这个宏的一个参数是一个文件名,这个文件应该在源代码目录中,它用于健全性检查,以保证configure脚本已正确定位源文件目录。

AM_CONFIG_HEADER指定了要创建的头文件,差不多总是config.h。创建的头文件包含由configure定义的C预处理符号。最低限度应该定义PACKAGE和VERSION符号,这样可以将应用程序名称和版本传送到代码中,而无须对它们硬编码(非公用的源文件应该包含 config.h(#include)以利用这些定义。然而,不要将config.h文件安装到系统中,因为它有可能与其他的软件包冲突)。

AM_INIT_AUTOMAKE初始化automake。传到这个宏里的参数是要编译的应用程序的名称和版本号(这些参数成为config.h中定义的PACKAGE和VERSION值)。

AM_MAINTAINER_MODE关闭缺省时仅供程序维护者使用的makefile目标,并修改以使configure能理解 –enable-maintainer-mode选项。–enable-maintainer-mode将maintaineronly目标重新打开。仅供维护者使用的makefile目标允许最终用户清除自动生成的文件,比如configure,这意味着要修复编译故障,必须安装有autoconf和automake软件。注意,因为autogen.sh脚本主要是给开发人员用的,autogen.sh会自动传递一个–enable- maintainer-mode选项给configure。

AM_ACLOCAL_INCLUDE指定一个附加的目录,用于搜索m4宏。在这里,它指定为macros子目录。在这个目录中应该有Gnome宏的拷贝。

GNOME_INIT给configure添加一个与Gnome相关的命令行参数个数,并为Gnome程序定义一些makefile变量,这些变量中包含了必要的预处理程序和链接程序标志。这些标志是由gnome-config脚本取得的。安装gnome-libs时会安装gnome- config脚本。

AC_PROG_CC定位C编译器。

AC_CHECK_LIB如果程序中加入了多线程用到的锁的话就要加入像 AC_CHECK_LIB([pthread], [main])这样的检测,这个宏的含义如下:

 

 
 

LIBS是link的一个选项,程序中使用了读写锁,所以要测试pthread库中是否存在pthread_rwlock_init函数。

AC_PROG_RANLIB如果是多线程的程序的话要加入这句话,要不运行automake命令时会出错。

AC_ISC_POSIX添加一些在某些平台上实现POSIX兼容需要的标志。

AC_HEADER_STDC检查当前平台上是否有标准的ANSI头文件,如果有,则定义STDC_HEADERS。

AC_ARG_PROGRAM添加一些选项到configure中,让用户能够修改安装程序的名称(如果在用户系统上碰巧有一个与要安装的程序名称相同的程序,这是很有用的)。

AM_PROG_LIBTOOL是由automake用来设置libtool的用途的。只在计划编译共享库或动态可加载模块时才需要设置这个值。

GNOME_COMPILE_WARNINGS给gcc命令行添加许多警告选项,但是在其他绝大多数的编译器上什么也不做。

ALL_LINGUAS=“es”不是一个宏,只是一句shell代码。它包含一个由空格分隔的语言种类缩写表,对应于po子目录下的.po文件。.po文件包含翻译成其他语言的文本,所以ALL_LINGUAS应该列出程序已经被翻译成的所有语言。

AM_GNU_GETTEXT由automake使用,但是这个宏会随gettext软件包发布。它让 automake执行一些与国际化相关的任务。

AC_SUBST输出一个变量到由configure生成的文件中。具体内容将在后面说明。

AC_OUTPUT列出由configure脚本创建的文件。这些文件都是由带.in后缀的同名文件生成的。例如,src/Makefile是由src/Makefile.in生成的,config.h是由config.h.in生成的。在执行AC_OUTPUT宏时,configure脚本处理包含有两个@符号标志的变量(例如@PACKAGE@)的文件。只有用AC_SUBST输出了变量,它才能识别这些变量(许多在上面讨论过的预先写好的宏都用AC_SUBST定义变量)。这些特征用于将一个Makefile.in文件转换成一个Makefile文件。典型情况下,Makefile.in 是由automake从Makefile.am(了解更多Makefile.am的写法请阅读《Makefile.am 规则和实例详解》)生成的(不过,你可以只用autoconf,而不用automake,自己编写一个 Makefile.in)。

上面例子中提到的宏都是基本配置和常用的,还有一些可以用到的宏大家有兴趣的话可以学习收集下,不过基本上上面所说的已经可以完成一次配置,不过这里写的一些配置大多数是收集于网络,希望大家和鹏博客共同学习提高,有不对的地方希望大家能之出,希望大家能支持。




作者:id被吃
链接:https://www.jianshu.com/p/befa2fc2420c
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

==================

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

 

一、生成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就是将已经编译好的文件进行了打包。