Linux使用源码安装软件示例

Linux使用源码安装软件示例

在Linux下安装一个软件可以使用rpm/yum安装也可以使用源码安装。

源码解压缩后放置在哪里?

生成的可执行文件放置在哪里?

……

下面看一个简单的使用源码安装软件的例子:

(注:这个例子来自于《鸟哥的Linux私房菜》,下面是我实践的过程)

Tarball:ntp-4.2.8p10.tar.gz

1.将Tarball放置在/root目录下:

[root@localhost ~]# ll
total 6928
-rw-------. 1 root root    2692 Apr 13  2017 anaconda-ks.cfg
……
-rw-r--r--. 1 root root 6998648 Apr 12 17:08 ntp-4.2.8p10.tar.gz
……

2.将Tarball解压缩在/usr/local/src下:

[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# tar zxvf /root/ntp-4.2.8p10.tar.gz 
ntp-4.2.8p10/
ntp-4.2.8p10/packageinfo.sh
ntp-4.2.8p10/README.pullrequests
ntp-4.2.8p10/configure
……

注意Tarball中包含的configure,其可以检测运行环境,生成makefile。

[root@localhost src]# ll
total 4
drwxrwsr-x. 23 427 6011 4096 Mar 21 07:47 ntp-4.2.8p10
[root@localhost src]# cd ntp-4.2.8p10/
[root@localhost ntp-4.2.8p10]# ll
total 7044
-rw-rw-r--.  1 427 6011   50020 Mar 21 07:37 aclocal.m4
drwxrwsr-x.  2 427 6011    4096 Mar 21 07:47 adjtimed
-rw-rw-r--.  1 427 6011     605 Jun 22  2011 bincheck.mf
-rwxrwxr-x.  1 427 6011    4801 Jun 17  2015 bootstrap
-rwxrwxr-x.  1 427 6011    4239 Dec 23  2014 build
-rw-rw-r--.  1 427 6011  242006 Mar 21 06:34 ChangeLog
-rw-rw-rw-.  1 427 6011     374 Jan  8  2012 check-libopts.mf
drwxrwsr-x.  2 427 6011    4096 Mar 21 07:47 clockstuff
-rw-rw-r--.  1 427 6011 5225786 Feb  1 04:41 CommitLog
-rw-rw-r--.  1 427 6011  195223 Jan  4  2007 CommitLog-4.1.0
drwxrwsr-x.  2 427 6011    4096 Jan 31 23:19 conf
-rw-rw-r--.  1 427 6011   42801 Mar 21 07:41 config.h.in
-rwxrwxr-x.  1 427 6011  937829 Mar 21 07:37 configure
……

3.使用configure检查环境并生成makefile:

[root@localhost ntp-4.2.8p10]# ./configure --prefix=/usr/local/ntp
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether make supports nested variables... (cached) yes
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking for style of include used by make... GNU
checking for cc... cc
……
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
……
configure: creating ./config.status
config.status: creating Makefile
config.status: creating adjtimed/Makefile
……

使用configure会检查编译器、链接器、头文件等等,最终生成makefile文件。

4.可以先make clean下(不是必要步骤,但是可以执行下,安全点):

[root@localhost ntp-4.2.8p10]# make clean
Making clean in sntp
make[1]: Entering directory `/usr/local/src/ntp-4.2.8p10/sntp'
Making clean in include
make[2]: Entering directory `/usr/local/src/ntp-4.2.8p10/sntp/include'
rm -rf .libs _libs
……

5.执行make进行源码编译:

[root@localhost ntp-4.2.8p10]# make
Compiling with GCC now generates lots of new warnings.

Don't be concerned. They're just warnings.

Don't send bug reports about the warnings, either.

Feel free to send patches that fix these warnings, though.

cd ./html && \
    ../scripts/build/checkHtmlFileDates
cd . && \
    ./scripts/build/checkChangeLog
make  all-recursive
make[1]: Entering directory `/usr/local/src/ntp-4.2.8p10'
Making all in sntp
……

6.make install安装到指定目录:

[root@localhost ntp-4.2.8p10]# make install
cd ./html && \
    ../scripts/build/checkHtmlFileDates
cd . && \
    ./scripts/build/checkChangeLog
make  install-recursive
make[1]: Entering directory `/usr/local/src/ntp-4.2.8p10'
Making install in sntp
make[2]: Entering directory `/usr/local/src/ntp-4.2.8p10/sntp'
[ ! -r ./../COPYRIGHT ]                 \
    || [ check-COPYRIGHT-submake -nt ./../COPYRIGHT ]   \
    || make  check-COPYRIGHT-submake
make  install-recursive
make[3]: Entering directory `/usr/local/src/ntp-4.2.8p10/sntp'
Making ins
[root@localhost ntp-4.2.8p10]# cd /usr/local
[root@localhost local]# ll
total 40
drwxr-xr-x. 2 root root 4096 Sep 23  2011 bin
drwxr-xr-x. 2 root root 4096 Sep 23  2011 etc
drwxr-xr-x. 2 root root 4096 Sep 23  2011 games
drwxr-xr-x. 2 root root 4096 Sep 23  2011 include
drwxr-xr-x. 2 root root 4096 Sep 23  2011 lib
drwxr-xr-x. 2 root root 4096 Sep 23  2011 libexec
drwxr-xr-x. 6 root root 4096 Apr 12 17:14 ntp
drwxr-xr-x. 2 root root 4096 Sep 23  2011 sbin
drwxr-xr-x. 5 root root 4096 Apr 13  2017 share
drwxr-xr-x. 3 root root 4096 Apr 12 17:09 src
[root@localhost local]# cd ntp
[root@localhost ntp]# ll
total 16
drwxr-xr-x. 2 root root 4096 Apr 12 17:14 bin
drwxr-xr-x. 2 root root 4096 Apr 12 17:14 libexec
drwxr-xr-x. 2 root root 4096 Apr 12 17:14 sbin
drwxr-xr-x. 5 root root 4096 Apr 12 17:14 share
[root@localhost ntp]# ll bin
total 6008
-rwxr-xr-x. 1 root root    2844 Apr 12 17:14 calc_tickadj
-rwxr-xr-x. 1 root root 2255174 Apr 12 17:14 ntpd
-rwxr-xr-x. 1 root root  384466 Apr 12 17:14 ntpdate
-rwxr-xr-x. 1 root root  791615 Apr 12 17:14 ntpdc
-rwxr-xr-x. 1 root root  666160 Apr 12 17:14 ntp-keygen
-rwxr-xr-x. 1 root root  832389 Apr 12 17:14 ntpq
-rwxr-xr-x. 1 root root  198198 Apr 12 17:14 ntptime
-rwxr-xr-x. 1 root root    3570 Apr 12 17:14 ntptrace
-rwxr-xr-x. 1 root root    3210 Apr 12 17:14 ntp-wait
-rwxr-xr-x. 1 root root  972854 Apr 12 17:14 sntp
-rwxr-xr-x. 1 root root   11333 Apr 12 17:14 tickadj
-rwxr-xr-x. 1 root root   11635 Apr 12 17:14 update-leap
[root@localhost ntp]# ll share/
doc/ man/ ntp/ 
[root@localhost ntp]# ll share/
total 12
drwxr-xr-x. 4 root root 4096 Apr 12 17:14 doc
drwxr-xr-x. 5 root root 4096 Apr 12 17:14 man
drwxr-xr-x. 3 root root 4096 Apr 12 17:14 ntp
[root@localhost ntp]# cd

上述执行时的输出贴了一堆,总结起来就是:

1.将源码解压在/usr/local/src目录下;

2.进入对应的目录,执行configure,注意prefix指定makefile中最后安装的目录,这个最后安装的目录通常是/usr/local/xxx,xxx是软件名;

3.执行make clean来先清除下“可能存在的.o文件”,实际上这一步一般不用也可以;

4.执行make编译源代码(注意,是编译,不是安装);

5.执行make install安装。

如果仅仅是安装到此为止就可以了,了解更多可以往下看看。

所谓的Tarball文件实际上是将源码先用tar打包,然后使用gzip或者bzip2压缩后得到的文件。

通常使用gzip压缩后得到的Tarball文件是:

xxx.tar.gz
xxx.tgz

通常使用bzip2压缩后得到的Tarball文件是:

xxx.tar.bz2

解压缩一个Tarball文件通常包含以下几项:

1.源代码文件;

2.configure/config检测程序;

3.自述文件README和安装说明INSTALL;

使用configure/config生成makefile然后即可安装。

注意:

make使用的是makefile或者Makefile,configure/config也是需要autoconfig这个软件来辅助的。

原本的Linux distribution发布安装的软件是在/usr中的,用户自行安装的软件建议放置在/usr/local下。

对于configure/config,如果没有显式地指明–prefix=/usr/local/xxx,那么默认将会是/usr/local。

对了,最后别忘记在/etc/man.config中加上刚装的软件的man路径。

你可能感兴趣的:(C-C++,LINUX)