The magic behind configure, make, make install

本文解析了Unix环境下使用./configure、make、make install命令安装软件的背后原理。从维护者的角度介绍了如何利用autotools套件创建配置脚本及Makefile模板,并通过一个简单的Hello World示例演示整个过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址:https://robots.thoughtbot.com/the-magic-behind-configure-make-make-install


If you’ve used any flavour of Unix for development, you’ve probablyinstalled 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 reallyunderstand what it meant, I just knew that if I wanted to install software thiswas the spell to recite.

Recently I’ve been building my own Unix tools, and I wanted to tap into thisstandard install process; not only is it familiar to many Unix users, it’s alsoa great starting point for building a package for Homebrew and the various Linuxand BSD package managers. It was time to dig into the Unix Grimoireand 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 onyour specific system. It makes sure all of the dependencies for the rest of thebuild and install process are available, and finds out whatever it needs to knowto use those dependencies.

Unix programs are often written in C, so we’ll usually need a C compiler tobuild them. In these cases the configure script will establish that yoursystem does indeed have a C compiler, and find out what it’s called and where tofind 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 finishedprogram from its source code.

The tarball you download usually doesn’t include a finished Makefile. Insteadit comes with a template called Makefile.in and the configure scriptproduces 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 totheir final destinations. The make install command will copy the builtprogram, and its libraries and documentation, to the correct locations.

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

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

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

Where do these scripts come from

All of this works because a configure script examines your system, and usesthe 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, youwill have seen that they are thousands of lines of dense shell script. Sometimesthese supporting scripts are longer than the source code of the program theyinstall.

Even starting from an existing configure script, it would be very daunting tomanually 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 ofprograms collectively referred to as autotools. Thissuite includes autoconf, automake, and many other programs, all of whichwork 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 upan install process that will run consistently on many different flavours ofUnix.

Hello world

Let’s take a simple “Hello world” C program, and see what it would take topackage 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 aconfigure.ac file written in m4sh—a combination of m4 macros and POSIXshell script—to describe what the configure script needs to do.

The first m4 macro we need to call is AC_INIT, which willinitialise autoconf and set up some basic information about the program we’repackaging. The program is called helloworld, the version is 0.1, and themaintainer 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 thatwith the AM_INIT_AUTOMAKE macro:

AM_INIT_AUTOMAKE

Next, we need to tell autoconf about the dependencies our configure script needsto look for. In this case, the configure script only needs to look for a Ccompiler. 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 discoverthem; for example the AC_PATH_PROG macro looks for a givenprogram on the user’s PATH.

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

The next line used the AC_CONFIG_FILES macro to tellautoconf that the configure script should do just that: it should find a filecalled Makefile.in, substitute placeholders like @PACKAGE_VERSION@ withvalues like 0.1, and write the results to Makefile.

AC_CONFIG_FILES([Makefile])

Finally, having told autoconf everything our configure script needs to do, wecan 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 scriptit’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 stillmissing something. Our configure script will expect a Makefile.in file thatit can substitute all of those system-specific variables into, but so far, we’venot created that file.

Creating the Makefile

As with the configure script, the Makefile.in template is very long andcomplex. So instead of writing it by hand, we write a shorter Makefile.amfile, 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 theproject. Since we’re not following the standard layout of a GNU project, we warnautomake that this is a foreign project:

AUTOMAKE_OPTIONS = foreign

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

bin_PROGRAMS = helloworld

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

The PROGRAMS suffix is called a primary. It tells automake whatproperties the helloworld file 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 tothe directory defined by the variable bindir. There are various directoriesdefined for us by autotools—including bindir, libdir, andpkglibdir—but we can also define our own.

If we wanted to install some Ruby scripts as part of our program,we could define a rubydir variable and tell automake to install ourRuby 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 nuanceautomake’s behaviour.

Since we’ve defined a PROGRAM, we need to tell automake where to find itssource files. In this case, the prefix is the name of the program these sourcefiles 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 theMakefile.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 thefinished 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 theconfigure script and Makefile.in without all of the files we used togenerate them.

Fortunately, autotools will help us with distribution too. The Makefile containsall kinds of interesting targets, including one to build a tarball of theproject 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 varietyof conditions:

make distcheck

Overview

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

内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值