CentOS7.6 源码安装 libxml2-2.8.0

获取libxml2

libxml官网:http://xmlsoft.org/

下载源码包地址:http://xmlsoft.org/downloads.html

本人下载的是libxml2-2.8.0版本的源码包,源码包文件名为:libxml2-2.8.0.tar.gz。获取源码包命令如下:

wget ftp://xmlsoft.org/libxml2/libxml2-2.8.0.tar.gz

下载完成后,查看源码包文件的md5值和sha256值:

# md5sum libxml2-2.8.0.tar.gz
c62106f02ee00b6437f0fb9d370c1093  libxml2-2.8.0.tar.gz

]# sha256sum libxml2-2.8.0.tar.gz
f2e2d0e322685193d1affec83b21dc05d599e17a7306d7b90de95bb5b9ac622a  libxml2-2.8.0.tar.gz

<注意> 安装需要有root权限,最好是在root用户下进行,如果不是root用户,执行编译安装操作时需要在命令前加上 sudo 。本人的所有操作是在root用户下进行的。

安装步骤

一、解压缩源码包:libxml2-2.8.0.tar.gz。

tar -xzvf libxml2-2.8.0.tar.gz

执行解压缩命令成功后,会在当前目录下生成libxml2-2.8.0目录。

二、进入libxml2-2.8.0目录,执行 configure 配置脚本。

1、通过执行:./configure --help 命令,可以查看configure脚本的使用帮助。默认是安装在/usr/local路径下,对应的可执行文件、库文件、头文件等路径是:/usr/local/bin、/usr/local/lib、/usr/local/include下。

2、我本人设置了自定义安装路径,执行命令如下:

./configure --prefix=/usr/local/libxml2

 三、第2步执行成功后,会生成Makefile文件,然后使用make命令进行源码编译。

make

 四、编译成功后,执行安装命令。

make install

 <Tips> 要卸载glib库,命令为:make uninstall

五、安装成功后,可以看到/usr/local目录下多了一个libxml2目录,查看一下这个目录的树形结构。

tree -L 2 libxml2
libxml2/
├── bin
│   ├── xml2-config
│   ├── xmlcatalog
│   └── xmllint
├── include
│   └── libxml2
├── lib
│   ├── libxml2.a
│   ├── libxml2.la
│   ├── libxml2.so -> libxml2.so.2.8.0
│   ├── libxml2.so.2 -> libxml2.so.2.8.0
│   ├── libxml2.so.2.8.0
│   ├── pkgconfig
│   ├── python2.7
│   └── xml2Conf.sh
└── share
    ├── aclocal
    ├── doc
    ├── gtk-doc
    └── man

11 directories, 9 files

   可以看到,libxml2目录下有4个一级子目录:bin  include  lib  share。这里就不再详细介绍这些子目录的具体内容了。接下来就是配置libxml2三方库的开发环境了。

配置libxml2库的开发环境

一、配置PATH环境变量

  PATH环境变量对应的可执行文件(一般设置的是Linux命令的路径)。这里我设置libxml2库的bin目录下的路径为对所有用户生效,因此需要在/etc/profile文件中配置PATH环境变量的值。

vim /etc/profile,添加内容如下:

#Add bin path
export PATH=$PATH:/usr/local/libxml2/bin

  保存修改后,执行:source /etc/profile,令修改生效,下同。尝试执行一个/usr/local/libxml2/bin目录下的Linux命令。例如,执行xml2-config命令,打印出libxml2三方库的版本信息:

xml2-config --version
2.8.0

 二、配置libxml2三方库的C头文件搜索路径

  C头文件搜索路径对应的环境变量是C_INCLUDE_PATH,这里我们只在当前用户的主目录下的.bash_profile文件中配置。执行命令:vim ~/.bash_profile,添加内容如下:

export C_INCLUDE_PATH=/usr/local/libxml2/include/libxml2:$C_INCLUDE_PATH

 保存修改后,执行:source ~/.bash_profile,令修改生效,下同。

三、配置libxml2三方库的链接库文件搜索路径

  执行命令:vim ~/.bash_profile,添加内容如下:

# Add third_lib path
export LD_LIBRARY_PATH=/usr/local/libxml2/lib:$LD_LIBRARY_PATH
export LIBRARY_PATH=/usr/local/libxml2/lib:$LIBRARY_PATH

至于,为什么要同时配置LIBRARY_PATHLD_LIBRARY_PATH 这两个环境变量,请参考下面链接中的博客说明。

【参考】

环境变量:LIBRARY_PATH 和 LD_LIBRARY_PATH的区别

 

四、配置man帮助手册搜索路径

   在libxml2的安装路径下有一个share子目录,而share目录下又有一个man目录,这个目录下存放的就是帮助手册文件。CentOS7.6系统是在/etc/man_db.conf下进行配置的。

一、配置libxml2三方库的API函数的帮助手册。对应的变量名为MANDATORY_MANPATH。找到MANDATORY_MANPATH,在其下面添加如下内容:

MANDATORY_MANPATH                       /usr/local/libxml2/share/man

 二、配置libxml2三方库的Linux命令的帮助手册。对应的变量名为MANPATH_MAP。找到MANPATH_MAP,在其下面添加如下内容:

MANPATH_MAP     /usr/local/libxml2/bin  /usr/local/glib/share/man

 保存修改。

三、测试man帮助命令。

3.1、使用man命令查看libxml2的使用帮助。例如,执行命令:man libxml,输出部分信息如下所示:

libxml(3)                                                       Library Functions Manual                                                      libxml(3)

NAME
       libxml - library used to parse XML files

DESCRIPTION
       The  libxml library is used to parse XML files.  Its internal document representation is as close as possible to the DOM (Document Object Model)
       interface, an API for accessing XML or HTML structured documents.

       The libxml library also has a SAX-like interface, which is designed to be compatible with expat(1).  NOTE: SAX, the Simple API  for  XML,  is  a
       standard interface for event-based XML parsing, developed collaboratively by the members of the XML-DEV mailing list, currently hosted by OASIS.
       The expat library is a XML 1.0 parser written in C, which aims to be fully conforming.  It is currently not a validating XML processor.

       The libxml library now includes a nearly complete XPath implementation.  The XPath (XML Path Language) is a language for addressing parts of  an
       XML document, designed to be used by both XSLT and XPointer.

       The libxml library exports Push and Pull type parser interfaces for both XML and html.

FILES
       /depot/lib/libxml_2.0.0/libxml.a
                             static library

       /depot/lib/libxml_2.0.0/libxml.so
                             shared library

       /depot/package/libxml_2.0.0/bin/xmllint
                             binary application for parsing XML files

AUTHORS
       Daniel  Veillard (daniel@veillard.com).  Red Hat Inc.  Manual page by Ziying Sherwin (sherwin@nlm.nih.gov), Lister Hill National Center for Bio‐
       medical Communications, U.S. National Library of Medicine.

SEE ALSO
       xmllint(1), libxslt(3), libexslt(3), xsltproc(1)

 3.2、使用man命令查看libxml2三方库相关的Linux命令的使用帮助信息。例如,执行命令:man xml2-config,输出部分信息如下:

GNOME-XML(1)                                                             1.1.0                                                             GNOME-XML(1)

NAME
       xml-config - script to get information about the installed version of GNOME-XML

SYNOPSIS
       xml-config [--prefix[=DIR]] [--libs] [--cflags] [--version] [--help]

DESCRIPTION
       xml-config is a tool that is used to determine the compile and linker flags that should be used to compile and link programs that use GNOME-XML.

OPTIONS
       xml-config accepts the following options:

       --version
               Print the currently installed version of GNOME-XML on the standard output.

       --libs  Print the linker flags that are necessary to link a GNOME-XML program.

       --cflags
               Print the compiler flags that are necessary to compile a GNOME-XML program.

       --prefix=PREFIX
               If specified, use PREFIX instead of the installation prefix that GNOME-XML was built with when computing the output for the --cflags and
               --libs options. This option must be specified before any --libs or --cflags options.

libxml2安装路径:/usr/local/libxml2/bin,有三个Linux命令:xml2-config  xmlcatalog  xmllint,我们现在可以使用man <command>的方式查看这些命令的使用帮助信息了。

 

五、测试一个libxml2程序

  我们尝试编译一个libxml2源程序,源文件名为:libxml2_version.c,源码如下:

/**
**程序描述:打印出libxml2三方库的版本信息。
*/
#include <stdio.h>
#include <libxml/xmlversion.h>

int main()
{
  printf("Current Libxml2 Version: %s\n", LIBXML_DOTTED_VERSION);
  return 0;
}

 编译命令:gcc libxml2_version.c -o libxml2_version -lxml2

运行结果:./libxml2_version
Current Libxml2 Version: 2.8.0

 

六、使用 pkg-config 方式配置libxml2三方库的搜索路径

  使用pkg-config方式配置三方库的搜索路径,主要是用于Makefile文件中编写程序的编译命令。对应的环境变量是PKG_CONFIG_PATH,配置值为xxx.pc文件的搜索路径。我们一般选择在当前用户的主目录下的.bash_profile配置文件中配置PKG_CONFIG_PATH环境变量的值。执行命令:vim ~/.bash_profile,添加内容如下:

#Add pkg-config path
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH

 在libxml2的安装路径下的/usr/local/libxml2/lib,有一个pkgconfig子目录,该目录下有一个.pc文件:libxml-2.0.pc,查看下该文件的内容:

prefix=/usr/local/libxml2
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
modules=1

Name: libXML
Version: 2.8.0
Description: libXML library version2.
Requires:
Libs: -L${libdir} -lxml2
Libs.private:  -lz  -lm
Cflags: -I${includedir}/libxml2

   这个libxml-2.0.pc文件描述了libxml2库的安装路径:prefix=/usr/local/libxml2;库文件路径、头文件路径,库名:libXML;版本号:2.8.0,链接的库文件搜索路径、头文件搜索路径,你会发现这些路径与我们上面自己手动配置的路径是一致的。

  由于我们将PKG_CONFIG_PATH环境变量的值配置为/usr/local/lib/pkgconfig路径下,因此我们需要将/usr/local/libxml2/lib/pkgconfig路径下的libxml-2.0.pc文件复制到该配置路径下。执行命令如下:

cd /usr/local/libxml2/lib/pkgconfig ; ls
cp libxml-2.0.pc /usr/local/lib/pkgconfig

 设置完成后,我们重新使用pkg-config 的方式来编译一下libxml2_version.c这个源程序,编译命令如下:

gcc `pkg-config --cflags --libs libxml-2.0` libxml2_version.c -o libxml2_version

 测试结果显示编译通过,运行结果与上面的是完全一样的。

<说明> --cflags 参数,可以给出编译时所需的头文件搜索路径。--libs 参数,可以给出编译时所需的库文件搜索路径。libxml-2.0对应的是/usr/local/lib/pkgconfig目录下的glib-2.0.pc文件。即,libxml-2.0 libxml-2.0.pc 文件是一种对应关系同时,可以注意到我们的编译命令并没有加 -lxml-2.0参数,但是加上也无妨。

<Tips> 建议在编写Makefile文件中,使用 pkg-config 的方式来设置三方库的开发环境,这样可以减少和规避潜在的开发风险。

【参考】

简述configure、pkg-config、pkg_config_path三者的关系

 

总结

  至此,libxml2-2.8.0三方库的源码安装和开发环境配置就完成了,接下来就可以使用libxml2库来进行应用程序的开发工作了。

 

posted @ 2020-06-08 20:08  Kugle  阅读(4433)  评论(0编辑  收藏  举报