libxml2 + iconv + msvc 在windows下生成使用庫
爲了跨平臺的解析xml,偶然獲得Linux下比較好用的一個xml的解析庫libxml2
,使用起來確實比較簡單,方便;但移植到windows下後發下使用上存在問題:
無法解析格式位GB2312
類型的XML文件,但由於一些外部因素,Windows下必須使用GB2312
格式
在網上查看了諸多教程,有很多編譯生成libxml2
庫的文章,但都不支持iconv
,仔細閱讀README
後,終於生成了可用的libxml2.lib
庫,記錄下來備用
操作系統:windows 7 x64
版本:libiconv-1.15, libxml2 Github版本
Visual Studio版本:vs2015
Cygwin:x64
以下操作皆爲生成x64位庫,32位的基本類似,可以查看具體的README。
一、生成iconv.lib
庫
下載 libiconv-1.xx.tar.gz
在1.15中,需要閱讀README.windows
,在1.16中需要閱讀INSTALL.windows
,其餘版本的類似,找到windows版本的安裝說明。
三種方式可以編譯安裝libiconv
庫,我這裏使用的是第二種,使用MSVC
安裝,這個是VS種安裝的C/C++工具鏈,一般都已經安裝。
There are three ways to create binaries of this package for Microsoft Windows:
- Native binaries, built using the mingw tool chain. 2) Native
- binaries, built using the MS Visual C/C++ tool chain. 3) Binaries for
- the Cygwin environment.
根據說明:
This recipe requires also a Cygwin environment (with 'bash', the common POSIX
commands, and 'make') as a build environment. Building with 'nmake' is not
supported.
For this, you need to install
* Cygwin (from https://cygwin.com/),
* some packages available from the Cygwin package installer:
make
You also need the scripts 'ar-lib' and 'compile' from
https://git.savannah.gnu.org/gitweb/?p=automake.git;a=blob_plain;f=lib/ar-lib;hb=HEAD
https://git.savannah.gnu.org/gitweb/?p=automake.git;a=blob_plain;f=lib/compile;hb=HEAD
respectively.
They may also be included in this package, in directory 'build-aux/'.
Save them; the instructions below assume that you stored them in $HOME/msvc/.
Make them executable:
chmod a+x ar-lib compile
這裏需要安裝Cygwin
安裝Cygwin
時需要選擇make
安裝包,同時需要下載ar-lib
和 compile
,並且將這兩個安裝包放到$HOME/msvc/
路徑下,如果沒有msvc路徑,需要創建,還需要改變相應的權限chmod a+x ar-lib compile
。
操作完成後,打開Cygwin
的bash
界面,根據需要,選擇x64或者x86版本的需求進行操作,也可以將所有的命令寫成一個shell腳本: x64.sh
,一次性執行,這裏以x64爲例,必須要注意的時,在編寫腳本的時候,一定要使用的時unix/linux
風格的文件類型,windows和linux的換行符不同,在Cygwin中執行會出問題:
#x64 版本:filename: x64.sh
# Windows C library headers and libraries.
WindowsCrtIncludeDir='C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt'
WindowsCrtLibDir='C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10240.0\ucrt\'
INCLUDE="${WindowsCrtIncludeDir};$INCLUDE"
LIB="${WindowsCrtLibDir}x64;$LIB"
# Windows API headers and libraries.
WindowsSdkIncludeDir='C:\Program Files (x86)\Windows Kits\8.1\Include\'
WindowsSdkLibDir='C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\'
INCLUDE="${WindowsSdkIncludeDir}um;${WindowsSdkIncludeDir}shared;$INCLUDE"
LIB="${WindowsSdkLibDir}x64;$LIB"
# Visual C++ tools, headers and libraries.
VSINSTALLDIR='C:\Program Files (x86)\Microsoft Visual Studio 14.0'
VCINSTALLDIR="${VSINSTALLDIR}"'\VC'
PATH=`cygpath -u "${VCINSTALLDIR}"`/bin/amd64:"$PATH"
INCLUDE="${VCINSTALLDIR}"'\include;'"${INCLUDE}"
LIB="${VCINSTALLDIR}"'\lib\amd64;'"${LIB}"
export INCLUDE LIB
PATH=/usr/local/msvc64/bin:$PATH
export PATH
#win32_target=_WIN32_WINNT_WINXP # for MSVC 9.0
#win32_target=_WIN32_WINNT_VISTA # possibly for MSVC >= 10.0
win32_target=_WIN32_WINNT_WIN7 # possibly for MSVC >= 10.0
#win32_target=_WIN32_WINNT_WIN8 # possibly for MSVC >= 10.0
./configure --host=x86_64-w64-mingw32 --prefix=/usr/local/msvc64 \
CC="$HOME/msvc/compile cl -nologo" \
CFLAGS="-MD" \
CXX="$HOME/msvc/compile cl -nologo" \
CXXFLAGS="-MD" \
CPPFLAGS="-D_WIN32_WINNT=$win32_target -I/usr/local/msvc64/include" \
LDFLAGS="-L/usr/local/msvc64/lib" \
LD="link" \
NM="dumpbin -symbols" \
STRIP=":" \
AR="$HOME/msvc/ar-lib lib" \
RANLIB=":"
make
make check
make install
在Cygwin
的命令行中執行命令:
./x64.sh
編譯執行的時間會比較久,執行完成後,在/usr/local/msvc64/
路徑下的include
文件夾中會生成iconv.h
頭文件,在lib
文件夾中會生成iconv.dll.lib
庫文件,需要將其重命名未iconv.lib
以供使用,至此iconv
靜態庫生成完畢,此外,在libiconv-1.xx/lib/.libs/
路徑下會生成iconv-2.dll
文件,此文件也需保留。
二、 生成libxml2
庫
首先在Github中下載了源文件後,解壓,進入路徑libxml2-master/win32
中,查看README文件,文件中比較詳細的說明了如何進行操作,可以將所有的操作步驟寫成一個批處理命令的腳本,即:.bat
或.cmd
文件: x64_debug_iconv_yes.bat
:: filename : x64_debug_iconv_yes.bat
:: 配置運行的環境, x64
call "c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64\vcvars64.bat"
:: 先清除之前安裝的所有配置,如果第一次,可以不執行
"c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\nmake.exe" clean
:: 配置信息
cscript configure.js compiler=msvc iconv=yes ^
prefix=c:\libxml2\x64\debug ^
binddir=c:\libxml2\x64\debug\bin ^
incdir=c:\libxml2\x64\debug\include ^
libdir=c:\libxml2\x64\debug\lib ^
sodir=c:\libxml2\x64\debug\dll ^
include=c:\cygwin64\usr\local\msvc64\include ^
lib=c:\cygwin64\usr\local\msvc64\lib ^
debug=yes
:: 編譯
"c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\nmake.exe" /f Makefile.msvc > debug_x64.log
:: 安裝
"c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\nmake.exe" /f Makefile.msvc install >> debug_x64.log
pause
關於cscript configure.js
的說明:
- 可以使用
cscript configure.js help
查看具體的幫助說明 prefix
表示需要將安裝的文件存放的位置bindir
表示安裝的可執行exe
存放位置,默認放在$prefix\bin
incdir
表示安裝的頭文件存放的位置,默認放在$prefix\include
libdir
表示生成的靜態庫存放的位置,默認放在$prefix\lib
sodir
表示生成的動態庫存放的位置,默認放在$prefix\bin
include
表示生成libxml2
庫需要依賴的庫的頭文件存放的位置,特指iconv
頭文件的路徑lib
表示生成libxml2
庫需要依賴的庫存放的位置,特指iconv.lib
的路徑
在cmd
中執行:
x64_debug_iconv_yes.bat
如果輸出沒有錯誤,則執行成功,生成libxml2.dll libxml2.lib庫,如果出錯,可以查看debug_x64.log
日誌文檔。
三、 libxml2 庫使用問題
- 當XML文件是GB2312格式時,運行時需要額外添加動態庫
iconv-2.dll