源码编译中configure&&make&&make install背后的魔力


=Start=

缘由:

接触Linux好几年了,从一开始的只会对着网上教程进行操作到现在已经能明白一些操作/命令背后的原理,这中间经历了很多。虽然现在在源码编译的过程中经常用到 ./configure && make && make install 操作,之前也有过一定的学习、了解,以为自己已经懂了,但最近在实际接触一些较大的工程时才发现自己知道的太少了,很多东西似懂非懂,需要重新深入学习一下。

正文:

参考解答:
源码编译和分发过程中的一些命令:

在开发/维护者的系统上:

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

在终端用户的系统上:

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

生成 Makefile 的来龙去脉:

首先进入源代码所在目录,然后在该目录下运行一系列命令,创建和修改几个文件,就可以生成符合该平台的Makefile文件,操作过程如下:

  1. 运行autoscan命令
  2. 将configure.scan 文件重命名为configure.in,并修改configure.in文件
  3. 在project目录下新建Makefile.am文件,并在core和shell目录下也新建makefile.am文件
  4. 在project目录下新建NEWS、 README、 ChangeLog 、AUTHORS文件
  5. 将/usr/share/automake-1.X/目录下的depcomp和complie文件拷贝到本目录下
  6. 运行aclocal命令
  7. 运行autoconf命令
  8. 运行automake -a命令
  9. 运行./confiugre脚本

具体流程图如下:


我的理解:

autoconf 命令是用于根据 configure.in 生成 configure 文件的;而 automake 命令是用于根据 Makefile.am 生成 Makefile.in 文件的。

Makefile.am是一种比Makefile更高层次的规则。

'source code' -> 'configure.scan' -> 'configure.in' -> 'configure'+'Makefile.in' -> 'Makefile'
  • ./configure 这一步一般用来生成 Makefile,同时检查后期在build和install时的依赖是否满足,以为下一步的编译做准备,你可以通过在 configure 后加上参数来对编译选项、安装位置进行控制;configure脚本还有一个功能就是根据模板 Makefile.in 生成 Makefile 文件;
  • make 是用来编译的,它从Makefile中读取指令,然后编译;
  • make install 是用来安装的,它也从Makefile中读取指令,安装到指定的位置。
参考链接:

The magic behind configure, make, make install
https://robots.thoughtbot.com/the-magic-behind-configure-make-make-install

What are Makefile.am and Makefile.in?
https://stackoverflow.com/questions/2531827/what-are-makefile-am-and-makefile-in

例解 autoconf 和 automake 生成 Makefile 文件
https://www.ibm.com/developerworks/cn/linux/l-makefile/

深入理解C语言
http://coolshell.cn/articles/5761.html

跟我一起写 Makefile(一)
http://blog.csdn.net/haoel/article/details/2886

如何调试makefile变量
http://coolshell.cn/articles/3790.html

如何系统地学习 Makefile 相关的知识(读/写)?
https://www.zhihu.com/question/23792247

=END=

, ,

《 “源码编译中configure&&make&&make install背后的魔力” 》 有 2 条评论

  1. Linux 程序编译过程的来龙去脉
    https://mp.weixin.qq.com/s/cE3Bxy3hBBlQRFTGMp6DgQ
    `
    本文将介绍如何将高层的C/C++语言编写的程序转换成为处理器能够执行的二进制代码的过程,包括四个步骤:
    预处理(Preprocessing)
    编译(Compilation)
    汇编(Assembly)
    链接(Linking)
    `

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注