How do I make a library installed with OPAM available to OCaml?
I followed this tutorial on OCaml FFI and installed Ctypes
via OPAM:
opam install ctypes
However OCaml doesn't find the module:
open Ctypes
(* ... *)
I am getting the error:
Unbound module Ctypes
Looks like I need to tell OCaml where is my Ctypes installation? Do I need to update some path variable so OCaml can look for my libraries installed via OPAM?
These are Ubuntu 15.04, OCaml 4.01.0, OPAM 1.2.0.
source to share
Installing anything on your system does not automatically make it visible to the compiler, this is true not only for OCaml, but for most common systems like C or C ++ to refer to multiple.
This means you need to pass some flags to the compiler or write Makefiles or use some project management system.
At OCaml, we have a fairly mature infrastructure that works great with opam
in particular. I don't want to go deep into explanations, just a quick overview.
ocamlfind
is used to find libraries on your system. It is somewhat close to pkg-config
the idea, but completely different in design. It wraps the compiler tools to pass options to them.
ocamlbuild
is a trendy Swiss knife that every OCamler should have in its arsenal. It is a tool that knows all the other tools and how to glue them together. I would say this is the preferred way to compile your projects, especially small ones.
oasis
close to autotools
in spirit, but not that generic and written in the premise that it should be very easy to use. Indeed, it is very easy, but still quite flexible and powerful.
Based on this review, we can go directly to your problem. This way you have installed ctypes. Now let's look at how the package is ctypes
displayed on your system from the point of view ocamlfind
. The easiest way is to list all packages visible to ocamlfind
and find ctypes
there:
$ ocamlfind list | grep ctypes
ctypes (version: 0.4.1)
ctypes.foreign (version: 0.4.1)
ctypes.stubs (version: 0.4.1)
ctypes.top (version: 0.4.1)
So it looks like there are 4 libraries under the umbrella of ctypes. One base library and some additional libraries that provide some functionality that is not needed by default.
Don't try to use them with ocamlbuild
ocamlbuild -package ctypes yourprogram.native
Or, without ocamlbuild
directly with ocamlfind
:
ocamlfind ocamlopt -package ctypes yourprogram.ml -o yourprogram.native
As you can see, there is an option package
to which you can pass the package name found ocamlfind
and it will automatically be made visible to the compiler.
source to share