Sqlite3-ocaml on Windows (Cygwin-64) - single exe

Using OCaml 4.12.0 for Windows (Cygwin-64 on Windows), I was able to get sqlite3-ocaml bindings working just fine. I was also able to create a sample OCaml sqlite3 application similar to [1].

For my OCaml sqlite3 application to work – the program needs access to sqlite3.dll, or in my case I had to rename it to libsqlite3-0.dll. However, my requirement is to deploy a single exe rather than the exe and the sqlite3 DLL.

Question: Is it possible to compile/link my OCaml program with sqlite3 so that the sqlite library is linked into the OCaml exe? Therefore I do not require a separate DLL file in my distribution.

I am able to create the sqlite3 DLL from source on Windows (Cygwin-64) [2]. My guess is I need to link the sqlite3 library with to my OCaml application.

Any input/hints would be greatly appreciated.
Many Thanks

References:
[1] GitHub - cedlemo/ocaml-sqlite3-notes: OCaml Sqlite3 tutorial
[2] How To Compile SQLite

Yes, this is possible and not very difficult: sqlite is distributed as a single .c file. To do what you want, you can either

  1. compile this single .c file as part of your C stubs, or

  2. compile this single .c file into a static library and link to it.

If using dune, 1) is done using the (foreign_stubs ...) field, and 2) is done using (foreign_library ...) stanza (to build the library) + (foreign_archives ...) field to link to it.

Cheers,
Nicolas

2 Likes

Nicolas – Thanks for pointing me in the right direction.
It is still not quite clear to me yet but I will keep digging.
Regards

If you are using Dune you may find Dealing with Foreign Libraries β€” Dune documentation useful.

If you are not using Dune you can just compile sqlite into an object file (gcc -c) or static library and pass it on the command-line of ocamlopt that links your executable.

Cheers,
Nicolas

I got it working!

Steps followed for info:

  1. Downloaded the sqlite3 C source code as an amalgamation
  2. Created a static library of sqlite3
gcc -c sqlite3.c
ar r libsqlite3.a sqlite3.o

copy libsqlite3.a to my OCaml project folder

  1. Add library in dune file – option 2 as per above suggestion

dune file

(executable
  (name sql)
  (libraries sqlite3)
  (foreign_archives sqlite3)
)

sql.ml – used sample from::

This is all on OCaml 4.12 for Windows (Cywin-64)

Thanks again.
Cheers

1 Like