How to build dbm.cma on Windows

Prerequisits

Prepare

Gather the needed files in one directory :

cldbm.c, dbm.ml, dbm.mli (from ocaml source otherlibs/dbm)
ndbm.h, gdbm-dll.h (from gdbm-1.8.3-1-src.zip)
libgdbm_compat.lib (from gdbm-1.8.3-1-lib.zip)
build.bat

@REM build.bat

@REM c stubs
ocamlc -verbose -ccopt -I. -ccopt -I%OCAMLLIB%\caml -c -o cldbm.obj cldbm.c
ocamlmklib -verbose -o _dbm_stubs libgdbm_compat.lib cldbm.obj

@REM cmi
ocamlc -c dbm.mli

@REM ocaml stubs
ocamlc -c dbm.ml
ocamlopt -c dbm.ml

@REM final library
ocamlc -verbose -a -custom -o dbm.cma dbm.cmo -dllib -ldll_dbm_stubs.dll -cclib -l_dbm_stubs
ocamlopt -verbose -a -o dbm.cmxa dbm.cmx -cclib -l_dbm_stubs

-I%OCAMLLIB%\caml is needed because cldbm.c references caml headers as #include <mlvalues.h> instead of #include <caml/mlvalues.h>. So you can patch four lines in cldbm.c and do not depend on OCAMLLIB environment variable.

Build

Just run build.bat

If everything works fine you will have

dbm.cmi with ocaml Dbm interface
lib_dbm_stubs.lib with C stubs needed for linking
dbm.cma for bytecode builds
dbm.cmxa and dbm.lib for native builds

Test app

dbm_test.ml

open Dbm
open Printf

let main () = 
  let db = opendbm "test" [Dbm_rdwr;Dbm_create] 511 in
  add db "test" "some data";
  add db "test2" "some data 2";
  add db "test3" "some data 3";
  print_endline (find db "test2");
  iter (fun key data -> printf "%s : %s\n%!" key data) db;
  close db

let _ = Printexc.print main ()

build.bat

ocamlc -verbose -o dbm_test.exe dbm.cma dbm_test.ml
ocamlopt -verbose -o dbm_test.native.exe dbm.cmxa dbm_test.ml

dbm3.dll and gdbm3.dll (from gdbm-1.8.3-1-bin.zip) needed at runtime


2008-11-10