Wednesday, July 03, 2013

How to build libxml2 on Windows using Visual Studio

Libxml2 depends on libiconv so that is the first thing to build. Like many GNU projects it is UNIX-centric, so building on Windows is not easy. However, some kind soul has created full instructions.

Once you have libiconv you will need to copy the built library from libiconv.lib to iconv.lib. This is due to a quirk in the libxml2 build where it assumes UNIX (whose linker removes the leading "lib" characters).

Once the libxml2 source tarball has been downloaded, unzipped and de-tar'd, you will see there are instructions in win32/Readme.txt. These instructions are quite good as far as they go, but there are still a few gotchas which are covered here.

The commands need to be issued from a DOS cmd prompt. Start one. Execute the following commands to set the window up for using the Visual Studio compiler. I am using Visual Studio 2005, aka VC8 (aka vc80).

set PATH=pathToLibiconvDLL;%PATH%

rem to pck up vcvarsall.bat
set PATH=C:\Program Files (x86)\Microsoft Visual Studio 8\VC;%PATH%

rem to pick up nmake and compiler
set PATH=C:\Program Files (x86)\Microsoft Visual Studio 8\VC\bin;%PATH%

cd win32
cscript configure.js compiler=msvc prefix=whereYouWantToInstall include=libiconvIncludeDir lib=libiconvLibDir debug=no

This will create an nmake file called Makefile.msvc. You need to edit it to set various compiler macros. Near line 65 you should see a line that looks like this:


Follow it with this line:


This will ensure that those nasty checked iterators are turned off along with other string checking nastiness that noone wants.
Then enter these commands:

nmake install

You will now have an install directory with subdirectories include, bin and lib.

The bin directory will contain several test programs and the utility xmllint. But there is a problem. The exes do not have the manifests embedded. For each executable you need to run the mt command to add the manifest to the exe. The format of the command is:

mt -manifest pathToManifestFile -outputresource:pathToExe;1

Note the semicolon one which is part of the command.

If you dont merge the manifest with the executable the first error you get is that msvcr80.dll cannot be found. You can also get the error R6034.


Martin (Drone) said...

thank you very much! with this tutorial i was able to get .pdb files by using debug=yes although i never used makefiles before.
one thing was a bit unclear: "include=libiconvIncludeDir lib=libiconvLibDir" needs to be the directories created in the tutorial mentioned at the top, not those from the downloaded libiconv package.

NobleUplift said...

I followed these directions for libxml2 2.9.4, but I'm getting this error message when I run nmake:

..\include\libxml/encoding.h(28) : fatal error C1083: Cannot open include file: 'iconv.h': No such file or directory

I'm seeing my Include Path and Lib Path after running the cscript, but I'm not seeing either path in the cl.exe. For the include directoy, it only has:

cl.exe /nologo /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "NOLIBTOOL" /W1 /I.. /I..\include /ID:\libxml2\release BINPREFIX=\bin\include /D "_REENTRANT" /D "HAVE_WIN32_THREADS" /D_CRT_SECURE_NO_DEPRECATE /D_CRT_NONSTDC_NO_DEPRECATE /D_SECURE_SCL=0 /D_SCL_SECURE_NO_WARNINGS /D_SCL_SECURE_NO_DEPRECATE /D "NDEBUG" /O2 /Foint.msvc\ /c ..\buf.c ..\c14n.c ..\catalog.c ..\debugXML.c ..\dict.c ..\DOCBparser.c ..\encoding.c ..\entities.c ..\error.c ..\globals.c ..\hash.c ..\HTMLparser.c ..\HTMLtree.c ..\legacy.c ..\list.c ..\nanoftp.c ..\nanohttp.c ..\parser.c ..\parserInternals.c ..\pattern.c ..\relaxng.c ..\SAX2.c ..\SAX.c ..\schematron.c ..\threads.c ..\tree.c ..\uri.c ..\valid.c ..\xinclude.c ..\xlink.c ..\xmlIO.c ..\xmlmemory.c ..\xmlreader.c ..\xmlregexp.c ..\xmlmodule.c ..\xmlsave.c ..\xmlschemas.c ..\xmlschemastypes.c ..\xmlwriter.c ..\xpath.c ..\xpointer.c ..\xmlstring.c

So I tried to put the libiconv headers in the libxml2 folder alongside the libxml2 header folder and it still didn't work. I'm also getting this error before the iconv.h error, don't know if they're related:

cl : Command line warning D9024 : unrecognized source file type 'BINPREFIX=\bin\include', object file assumed
cl : Command line warning D9027 : source file 'BINPREFIX=\bin\include' ignored

Any help is appreciated. I've had this blog post open since May 1st and I've since procrastinated on building libxml2 because I knew I would have an issue just like this lol.

Anonymous said...

Hi! I could have sworn I've visited this web site before but after going through some of the articles I realized it's new to me.
Regardless, I'm certainly delighted I discovered it and
I'll be book-marking it and checking back frequently!