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

**URL:** <https://discuss.ocaml.org/t/sqlite3-ocaml-on-windows-cygwin-64-single-exe/9725>\
**Category:** Learning\
**Tags:** sqlite\
**Created:** [April 24, 2022, 8:41pm UTC](https://discuss.ocaml.org/t/sqlite3-ocaml-on-windows-cygwin-64-single-exe/9725 "2022-04-24T20:41:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Willem](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@Willem](https://discuss.ocaml.org/u/Willem)\
**Post date:** [April 24, 2022, 8:41pm UTC](https://discuss.ocaml.org/t/sqlite3-ocaml-on-windows-cygwin-64-single-exe/9725/1 "2022-04-24T20:41:19Z")

</div>

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](https://github.com/cedlemo/ocaml-sqlite3-notes)  
[2] [How To Compile SQLite](https://www.sqlite.org/howtocompile.html#building_a_windows_dll)

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [April 24, 2022, 10:32pm UTC](https://discuss.ocaml.org/t/sqlite3-ocaml-on-windows-cygwin-64-single-exe/9725/2 "2022-04-24T22:32:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Willem](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@Willem](https://discuss.ocaml.org/u/Willem)\
**Post date:** [April 25, 2022, 10:04pm UTC](https://discuss.ocaml.org/t/sqlite3-ocaml-on-windows-cygwin-64-single-exe/9725/3 "2022-04-25T22:04:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [April 25, 2022, 10:30pm UTC](https://discuss.ocaml.org/t/sqlite3-ocaml-on-windows-cygwin-64-single-exe/9725/4 "2022-04-25T22:30:32Z")

</div>

> [@Willem](#):
>
> I will keep digging.

If you are using Dune you may find [Dealing with Foreign Libraries — Dune documentation](https://dune.readthedocs.io/en/latest/foreign-code.html#foreign-sandboxing) 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

---

<div class="post-metadata">

**Author:** ![Willem](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@Willem](https://discuss.ocaml.org/u/Willem)\
**Post date:** [April 26, 2022, 5:53pm UTC](https://discuss.ocaml.org/t/sqlite3-ocaml-on-windows-cygwin-64-single-exe/9725/5 "2022-04-26T17:53:27Z")

</div>

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

```auto
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

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

```

sql.ml – used sample from::

> <https://github.com/cedlemo/ocaml-sqlite3-notes/blob/master/samples/sample_3.ml>

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

Thanks again.  
Cheers
