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:

CFLAGS = $(CFLAGS) /D_CRT_SECURE_NO_DEPRECATE /D_CRT_NONSTDC_NO_DEPRECATE

Follow it with this line:

CFLAGS = $(CFLAGS) /D_SECURE_SCL=0 /D_SCL_SECURE_NO_WARNINGS /D_SCL_SECURE_NO_DEPRECATE

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

vcvarsall.bat
nmake
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.