Compiling pcre-ocaml with Visual Studio 2008
By gildor on Friday, October 22 2010, 13:00 - Permalink
One big change in the recent OASIS v0.2 release is the replacement of Str by Pcre. The big advantage of Pcre is that it can be used in multi-threaded environment, whereas it is not recommended to do so with Str. Since OASIS is used by the OCsigen part of OASIS-DB, we need to make it work safely with Lwt with multiple users at the same time.
Note that, we can probably use Str directly with Lwt, because it is not really multi-threaded. But we want to be safe on this point and Pcre is a very powerful library.
But the OCaml Pcre library depends on an external C library (pcre). This is not a problem on Linux et al, where it is shipped with the OS by default. But on Windows, you need to build it yourself. We want to do it using Microsoft Visual Studio 2008, mainly because OCaml was compiled with it -- and it seems the most natural way to build C library on Windows. As usual, building Open Source C libraries using MS Visual Studio, is not the most common way to process. However, the use of CMake enables a simple way to do it. Here is how we can compile pcre and pcre-ocaml for Windows:
- Download pcre and unzip it. We use 7.7, which is not the latest version, but the one matching GODI configuration
- Create c:\pcre\build and c:\pcre\pcre-7.7, beware that you should avoid spaces in the name, there is a small bug in OCaml with spaces for C options
- Download and install cmake
- Run cmake-gui and point the sources to the location of your unzipped pcre directory
- Run Configure a first time
- Choose Visual Studio 9 2008 generator
- You should get the configuration display as following:
- Change the CMAKEINSTALLPREFIX to c;\pcre\pcre-7.7, don't select BUILDSHAREDLIBS
- Run Configure again
- Run Generate
- Start MS Visual Studio 2008
- Open the PCRE solution located in c;\pcre\build
- Choose Solution: Release
- Generate the target ALL_BUILD
- If you don't have already administrator rights, restart Visual Studio with administrator rights
- Generate the target INSTALL
- Go to c:\pcre\pcre-7.7\lib and copy pcre.lib to libpcre.lib, to avoid name clashes with the future OCaml library
- Download and unzip pcre-ocaml
- Start a Visual Studio command line and run a cygwin shell inside it (e.g. my script to run cygwin shell with Visual Studio 2008)
- Change directory to lib directory of the unzipped source of pcre-ocaml
- Apply this patch, if it is not already applied
- Edit make_msvc.bat and set the variables PCREH and PCRELIB to good values
- Run make_msvc.bat
The best way to test your newly created OCaml pcre library is to try to compile an example from the examples directory of pcre-ocaml itself.
Sylvain Le Gall is an OCaml consultant at OCamlCore SARL
Thanks for the tip. This is cleaner than what I did here to build pcre-ocaml.
I also checked that the C library is correctly built with /MD instead of /MT.