源码编译安装

2016-11-21

源码编译安装

在日常管理工作中,大部分软件都是通过源码安装的。安装一个源码包,是需要我们自己把源代码编译成二进制的可执行文件。使用源代码的好处有可以自定义修改源代码,还可以定制相关的功能,因为源代码在编译时是可以附加额外的选项的。

源码包的编译用到了 Linux 系统的编译器,常见的源码包一般都是用 C 语音开发的,这也是因为 C 语音是 Linux 上最标准的程序语言。Linux 上的 C 语音编译器叫做 gcc,利用它就可以把 C 语音变成可执行的二进制文件。可以使用 yum install -y gcc 来安装。

安装一个源码包,通常需要三个步骤:

1) ./configure

在这一步,可以定制功能,加上相应的选项即可,具体有什么选项可以通过 ./configure --help 命令来查看。在这一步会自动检测系统与相关的套件是否有编译该源码包所需要的库,只有检测通过才会生成一个 Makefile 文件。

2) make

使用这个命令会根据 Makefile 文件中预设的参数进行编译,这一步其实就是 gcc 在工作了。

3) make install

安装步骤,生成相关的软件存放目录和配置文件的过程。

源码包的安装并非具有一定的标准安装步骤,这就需要在拿到源码包解压后,然后进入到目录找相关的帮助文档。通常会以 INSTALL 或 README 为文件名。

下载一个源码包

下载源码包最好到官方站点去下载,因为随便下载的源码包很有可能是被人修改过的。

[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# ls
[root@localhost src]# wget http://mirrors.aliyun.com/apache/httpd/httpd-2.2.31.tar.bz2

上面的地址是阿里云的镜像网站地址,国内下载速度比较快。在下载之前进入到了”/usr/local/src“目录,这是因为方便自己和其他管理员维护。所以,下载的源码包统一放到这个目录下吧。

解压源码包

[root@localhost src]# tar jxvf httpd-2.2.31.tar.bz2

配置相关的选项,并生成 Makefile

[root@localhost src]# cd httpd-2.2.31
[root@localhost httpd-2.2.31]# ./configure --help |less
`configure' configures this package to adapt to many kinds of systems.
Usage: ./configure [OPTION]... [VAR=VALUE]...
To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.
Defaults for the options are specified in brackets.
Configuration:
  -h, --help              display this help and exit
      --help=short        display options specific to this package
      --help=recursive    display the short help of all the included packages
  -V, --version           display version information and exit
  -q, --quiet, --silent   do not print `checking ...' messages
      --cache-file=FILE   cache test results in FILE [disabled]
  -C, --config-cache      alias for `--cache-file=config.cache'
  -n, --no-create         do not create output files
      --srcdir=DIR        find the sources in DIR [configure dir or `..']
Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local/apache2]
:

后面的内容未显示,使用 ./configure --help 命令查看可以使用的选项。一般常用的有 --prefix=PREFIX 这个选项的意思是定义软件包安装到哪里。通常源码包都是安装到 /usr/local 目录下的。比如,这个 apache 安装在 /usr/local/apache2 下。

[root@localhost httpd-2.2.31]# ./configure --prefix=/usr/local/apache2
checking for chosen layout... Apache
checking for working mkdir -p... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
Configuring Apache Portable Runtime library ...
checking for APR... reconfig
configuring package in srclib/apr now
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
Configuring APR library
Platform: x86_64-unknown-linux-gnu
checking for working mkdir -p... yes
APR Version: 1.5.2
checking for chosen layout... apr
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/usr/local/src/httpd-2.2.31/srclib/apr':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
configure failed for srclib/apr

很不幸,报错了,根据提示可知没有 gcc 编译器,安装一下

[root@localhost httpd-2.2.31]# yum install -y gcc

安装完成后继续

[root@localhost httpd-2.2.31]# ./configure --prefix=/usr/local/apache2

验证这一步是否成功的命令是:

[root@localhost httpd-2.2.31]# echo $?
0

返回值如果是”0“则执行成功,否则就是没有成功。此时就生成了 Makefile 了。

[root@localhost httpd-2.2.31]# ls -l Makefile
-rw-r--r-- 1 root root 8954 5月  15 09:25 Makefile
   **进行编译**
[root@localhost httpd-2.2.31]# make

这一步有可能提示没有发现 make 命令,需要安装一下,yum install -y make 。继续 make 后,会有很多类似乱七八糟的信息,编译的时间比较长,CPU 使用率会很高,这是因为 CPU 高速计算。编译完后,再使用 echo $? 验证是否正常安装。

[root@localhost httpd-2.2.31]# echo $?
0
[root@localhost httpd-2.2.31]# make install
[root@localhost httpd-2.2.31]# echo $?
0
[root@localhost httpd-2.2.31]# ls /usr/local/apache2/
bin    cgi-bin  error   icons    lib   man     modules
build  conf     htdocs  include  logs  manual

"/usr/local/apache2"下增加了好多目录。到此,apache 的源码包安装就完成了,在日常的源码安装工作中,遇到错误不能完成安装的情况很多,通常都是缺少某一个库文件所致。这时需要仔细查看报错信息或者查找当前目录下的"config.log"去得到相关的信息。或者。。搜索引擎


标题:源码编译安装
作者:散宜生
地址:https://17kblog.com/articles/2016/11/21/1479727945856.html