原文:https://robots.thoughtbot.com/the-magic-behind-configure-make-make-install#where-do-these-scripts-come-from

The magic behind configure, make, make install

January 19, 2015

 
+

If you’ve used any flavour of Unix for development, you’ve probably installed software from source with this magic incantation:

+
./configure
make
make install
+

I know I’ve typed it a lot, but in my early days using Linux I didn’t really understand what it meant, I just knew that if I wanted to install software this was the spell to recite.

+

Recently I’ve been building my own Unix tools, and I wanted to tap into this standard install process; not only is it familiar to many Unix users, it’s also a great starting point for building a package for Homebrew and the various Linux and BSD package managers. It was time to dig into the Unix Grimoire and find out what the incantation does.

What does all of this do

+

There are three distinct steps in this process:

+

1. Configure the software

+

The configure script is responsible for getting ready to build the software on your specific system. It makes sure all of the dependencies for the rest of the build and install process are available, and finds out whatever it needs to know to use those dependencies.

+

Unix programs are often written in C, so we’ll usually need a C compiler to build them. In these cases the configure script will establish that your system does indeed have a C compiler, and find out what it’s called and where to find it.

+

2. Build the software

+

Once configure has done its job, we can invoke make to build the software. This runs a series of tasks defined in a Makefile to build the finished program from its source code.

+

The tarball you download usually doesn’t include a finished Makefile. Instead it comes with a template called Makefile.in and the configure script produces a customised Makefile specific to your system.

+

3. Install the software

+

Now that the software is built and ready to run, the files can be copied to their final destinations. The make install command will copy the built program, and its libraries and documentation, to the correct locations.

+

This usually means that the program’s binary will be copied to a directory on your PATH, the program’s manual page will be copied to a directory on your MANPATH, and any other files it depends on will be safely stored in the appropriate place.

+

Since the install step is also defined in the Makefile, where the software is installed can change based on options passed to the configure script, or things the configure script discovered about your system.

+

Depending on where the software is being installed, you might need escalated permissions for this step so you can copy files to system directories. Using sudo will often do the trick.

Where do these scripts come from

+

All of this works because a configure script examines your system, and uses the information it finds to convert a Makefile.in template into a Makefile, but where do the configure script and the Makefile.in template come from?

+

If you’ve ever opened up a configure script, or associated Makefile.in, you will have seen that they are thousands of lines of dense shell script. Sometimes these supporting scripts are longer than the source code of the program they install.

+

Even starting from an existing configure script, it would be very daunting to manually construct one. Don’t worry, though: these scripts aren’t built by hand.

+

Programs that are built in this way have usually been packaged using a suite of programs collectively referred to as autotools. This suite includes autoconfautomake, and many other programs, all of which work together to make the life of a software maintainer significantly easier. The end user doesn’t see these tools, but they take the pain out of setting up an install process that will run consistently on many different flavours of Unix.

Hello world

+

Let’s take a simple “Hello world” C program, and see what it would take to package it with autotools.

+

Here’s the source of the program, in a file called main.c:

+
#include <stdio.h>

int
main(int argc, char* argv[])
{
printf("Hello world\n");
return 0;
}

Creating the configure script

+

Instead of writing the configure script by hand, we need to create a configure.ac file written in m4sh—a combination of m4 macros and POSIX shell script—to describe what the configure script needs to do.

+

The first m4 macro we need to call is AC_INIT, which will initialise autoconf and set up some basic information about the program we’re packaging. The program is called helloworld, the version is 0.1, and the maintainer is george@thoughtbot.com:

+
AC_INIT([helloworld], [0.1], [george@thoughtbot.com])
+

We’re going to use automake for this project, so we need to initialise that with the AM_INIT_AUTOMAKE macro:

+
AM_INIT_AUTOMAKE
+

Next, we need to tell autoconf about the dependencies our configure script needs to look for. In this case, the configure script only needs to look for a C compiler. We can set this up using the AC_PROG_CC macro:

+
AC_PROG_CC
+

If there were other dependencies, then we’d use other m4 macros here to discover them; for example the AC_PATH_PROG macro looks for a given program on the user’s PATH.

+

Now that we’ve listed our dependencies, we can use them. We saw earlier that a typical configure script will use the information it has about the user’s system to build a Makefile from a Makefile.in template.

+

The next line used the AC_CONFIG_FILES macro to tell autoconf that the configure script should do just that: it should find a file called Makefile.in, substitute placeholders like @PACKAGE_VERSION@ with values like 0.1, and write the results to Makefile.

+
AC_CONFIG_FILES([Makefile])
+

Finally, having told autoconf everything our configure script needs to do, we can call the AC_OUTPUT macro to output the script:

+
AC_OUTPUT
+

Here’s the whole thing. Not bad, compared to the 4,737 line configure script it’s going to produce!

+
AC_INIT([helloworld], [0.1], [george@thoughtbot.com])
AM_INIT_AUTOMAKE
AC_PROG_CC
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
+

We’re almost ready to package up and distribute our program, but we’re still missing something. Our configure script will expect a Makefile.in file that it can substitute all of those system-specific variables into, but so far, we’ve not created that file.

Creating the Makefile

+

As with the configure script, the Makefile.in template is very long and complex. So instead of writing it by hand, we write a shorter Makefile.am file, which automake will use to generated the Makefile.in for us.

+

First, we need to set some options to tell automake about the layout of the project. Since we’re not following the standard layout of a GNU project, we warn automake that this is a foreignproject:

+
AUTOMAKE_OPTIONS = foreign
+

Next, we tell automake that we want the Makefile to build a program called helloworld:

+
bin_PROGRAMS = helloworld
+

There’s a lot of information packed into this line, thanks to automake’s uniform naming scheme.

+

The PROGRAMS suffix is called a primary. It tells automake what properties the helloworldfile has. For example, PROGRAMS need to be built, whereas SCRIPTS and DATA files don’t need to be built.

+

The bin prefix tells automake that the file listed here should be installed to the directory defined by the variable bindir. There are various directories defined for us by autotools—including bindirlibdir, and pkglibdir—but we can also define our own.

+

If we wanted to install some Ruby scripts as part of our program, we could define a rubydirvariable and tell automake to install our Ruby files there:

+
rubydir = $(datadir)/ruby
ruby_DATA = my_script.rb my_other_script.rb
+

Additional prefixes can be added before the install directory to further nuance automake’s behaviour.

+

Since we’ve defined a PROGRAM, we need to tell automake where to find its source files. In this case, the prefix is the name of the program these source files build, rather than the place where they will be installed:

+
helloworld_SOURCES = main.c
+

Here’s the whole Makefile.am file for our helloworld program. As with theconfigure.ac and the configure script, it’s a lot shorter than the Makefile.in that it generates:

+
AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS = helloworld
helloworld_SOURCES = main.c

Putting it all together

+

Now we’ve written our config files, we can run autotools and generate the finished configure script and Makefile.in template.

+

First, we need to generate an m4 environment for autotools to use:

+
aclocal
+

Now we can run autoconf to turn our configure.ac into a configure script, and automake to turn our Makefile.am into a Makefile.in:

+
autoconf
automake --add-missing

Distributing the program

+

The end user doesn’t need to see our autotools setup, so we can distribute the configurescript and Makefile.in without all of the files we used to generate them.

+

Fortunately, autotools will help us with distribution too. The Makefile contains all kinds of interesting targets, including one to build a tarball of the project containing all of the files we need to distribute:

+
./configure
make dist
+

You can even test that the distribution tarball can be installed under a variety of conditions:

+
make distcheck

Overview

2

Now we know where this incantation comes from and how it works!

+

On the maintainer’s system:

+
aclocal # Set up an m4 environment
autoconf # Generate configure from configure.ac
automake --add-missing # Generate Makefile.in from Makefile.am
./configure # Generate Makefile from Makefile.in
make distcheck # Use Makefile to build and test a tarball to distribute
+

On the end-user’s system:

+
./configure # Generate Makefile from Makefile.in
make # Use Makefile to build the program
make install # Use Makefile to install the program

The magic behind configure, make, make install的更多相关文章

  1. configure, make, make install都做了什么

    1. 我的理解./configure:  确保接下来的make以及make install所依赖的文件没有问题make:  build编译连接生成可执行程序make install: 将编译好的可执行 ...

  2. Linux下编译安装源码包软件 configure ,make, make install, make test/check, make clean

    http://www.360doc7.net/wxarticlenew/541275971.html 一.什么是源码包软件? 顾名思义,源码包就是源代码的可见的软件包,基于Linux和BSD系统的软件 ...

  3. Linux下编译安装源码包软件 configure ,make, make install, make test/check, make clean 假目标

    http://www.360doc7.net/wxarticlenew/541275971.html 一.程序的组成部分 Linux下程序大都是由以下几部分组成: 二进制文件:也就是可以运行的程序文件 ...

  4. [转]./configure,make,make install的作用

    ./configure,make,make install的作用(转) 这些都是典型的使用GNU的AUTOCONF和AUTOMAKE产生的程序的安装步骤. ./configure是用来检测你的安装平台 ...

  5. [linux笔记]理清linux安装程序用到的(configure, make, make install)

    我作为一名经常和linux打交道的程序员,每次在linux安装软件都祈求可以用——apt-get,yum,brew等应用程序管理器安装,有的时候事与愿违,你只能自己编译安装-wtf,说好的美丽世界呢? ...

  6. 【转】./configure && make && make install详解

    在Linux中利用源码包安装软件最重要的就是要仔细阅读安装包当中的README  INSTALL两个说明文件,这两个文件会清楚的告诉你如何可以正确的完成这个软件的安装!          我们都知道源 ...

  7. 【转】configure/make/make install的使用说明

    这些都是典型的使用GNU的AUTOCONF和AUTOMAKE产生的程序的安装步骤. ./configure是用来检测你的安装平台的目标特征的.比如它会检测你是不是有CC或GCC,并不是需要CC或GCC ...

  8. Linux中的configure,make,make install到底在做些什么

    在Linux下经常要安装部署一些软件包或者工具,拿到安装包之后一看,简单,configure,make, make install即可搞定. 有时候我就在想,这个configure,make ,mak ...

  9. ./configure && make && make install 编译安装和卸载 (Linux)

    ./configure && make && make install 编译安装和卸载 (Linux) 正常的编译安装/卸载:   源码的安装一般由3个步骤组成:配置( ...

随机推荐

  1. 获取radio的值

    随着Jquery的作用越来越大,使用的朋友也越来越多.在Web中,由于CheckBox.Radiobutton .DropDownList等控件使用的频率比较高,就关系到这些控件在Jquery中的操作 ...

  2. POJ 2350 Above Average

    Description It is said that 90% of frosh expect to be above average in their class. You are to provi ...

  3. 一行一行分析JQ源码学习笔记-01

    jQuery (function(window,undefined){ }) undefined 防止外部参数中 var  undefined =10:以此来改变内部 undefined 不太建议用严 ...

  4. 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429

    素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: h ...

  5. 用Bash脚本将Linux普通用户添加为系统管理员

    将Linux普通用户添加为系统管理员在Gnome或KDE这样强大与完善的桌面环境下是非常简单的事情,一般来说在用户设置的对话框里就直接有相应选项.不过,出于简洁与高效的风格,自己目前并未使用这些高端但 ...

  6. 基于C++的顺序表的实现

    顺序表,是数据结构中按顺序方式存储的线性表,又称向量.具有方便检索的特点.以下,是笔者学习是基于C++实现的顺序表代码,贴上来当网页笔记用. #include <iostream> usi ...

  7. linux服务器被攻击处理过程

    开始排查 首先检查日志,以前做过安全运维,所以写过类似于检查命令和工具,开始一一排查. #查看是否为管理员增加或者修改 find / -type f -perm #显示文件中查看是否存在系统以外的文件 ...

  8. libPods.a 无法找到的解决方法

    http://stackoverflow.com/questions/9863836/library-not-found-for-lpods To be clear for newbies out t ...

  9. spring AOP 代理机制、执行过程、四种实现方式及示例详解

    1.加载过程 spring首先检测配置文件中的代理配置,然后去加载bean; 如果配置文件中没有配置代理,自然代理不会生效,如果配置了代理,但是代理还没有生效,那么有可能是加载顺序的问题,即在检测到代 ...

  10. php学习的第8天

    晚上老师布置啦17题算数问题的作业 我一看 感觉挺容易的 动手就开始做啦起来 不知不觉也9点多啊 终于做好前部题目 发现这我3个月前也遇到类似的题目 那是我还一题都做一题用上1个钟左右 好多也做不出 ...