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庫來進行應用程序的開發工作了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM