Linux - 如何生成configure文件
简介
本文简单地介绍一下如何生成configure文件,主要目的是把握整体的流程,以后如果要用到再仔细阅读对应文档。
整体的过程
在Linux下面手动编译什么东西的时候,一般都是下面三步:
./configure
make
make install
其中configure的作用是根据Makefile.in
生成Makefile,然后才能make。要生成configure
文件,需要用到一套工具,叫做autotools。
autotools的简单工作流程如下:
+---------+
| |
| |
Makefile.am +----> | automake| +-------> Makefile.in +-----+
| | |
| | |
| | |
+---------+ |
|
+---------+ |
| | |
| | v
configure.ac +----> | autoconf| +-------> configure +------+-----> Makefile
| |
| |
| |
+---------+
autotools包括automake, autoconf
以及其他一些工具。automake
用于生成Makefile
的模板Makefile.in
,autoconf
用于生成configure
文件。
示例
首先准备一下源文件:
#include <stdio.h>
int
main(int argc, char* argv[])
{
printf("Hello world\n");
return 0;
}
编写configure.ac
文件:
AC_INIT([helloworld], [0.2], [me@google.com]) # 程序的名字,版本,开发者
AM_INIT_AUTOMAKE # 初始化automake
AC_PROG_CC # 使用C编译器
AC_CONFIG_FILES([Makefile]) # configure的输出文件
AC_OUTPUT # 输出
然后编写Makefile.am
文件:
AUTOMAKE_OPTIONS = foreign #这是一个foreign项目,具体见参考中GNU automake文档List of Automake options
bin_PROGRAMS = helloworld #输出的二进制
helloworld_SOURCES = main.c #源文件列表
三个文件都准备好了之后,依次运行:
$ aclocal
$ autoconf
$ automake --fix-missing
然后就会发现Makefile.in
以及configure
都已经生成了。
参考:
- https://thoughtbot.com/blog/the-magic-behind-configure-make-make-install
- GNU automake