How to build dbm.cma on Windows



Gather the needed files in one directory :

cldbm.c,, dbm.mli (from ocaml source otherlibs/dbm)
ndbm.h, gdbm-dll.h (from
libgdbm_compat.lib (from

@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
ocamlopt -c

@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.


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

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 ()


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

dbm3.dll and gdbm3.dll (from needed at runtime