I followed this tutorial on OCaml FFI and installed Ctypes
through OPAM:
opam install ctypes
However, OCaml does not find the module:
open Ctypes
(* ... *)
I receive the error:
Unbound module Ctypes
It looks like I need to let OCaml know where my Ctypes installation is? Do I need to update some path variable to let OCaml look for my libraries installed through OPAM?
This is Ubuntu 15.04, OCaml 4.01.0, OPAM 1.2.0.
Your libraries are installed to the directory returned by opam var lib , which is by default ~/. opam/<switch>/lib .
You need to disable the sandbox by using opam init --disable-sandboxing . bwrap is the tool that is creating sandboxes.
Use opam switch create [name] <package-or-version> to switch to a different compiler.
Initialise the opam state, or update opam init options The init command initialises a local "opam root" (by default, ~/. opam/) that holds opam's data and packages. This is a necessary step for normal operation of opam.
Installing something on your system doesn't make it automatically visible for the compiler, this is true not only for OCaml, but for most conventional systems, like C or C++ to name a few.
That means that you need to pass some flags to the compiler, or to write Makefiles, or to use some project management systems.
In OCaml we have quite a mature infrastructure that plays very well with opam
in particular. I do not want to go very deeply in explanations, just a fast overview.
ocamlfind
tool is used to find libraries on your system. It is somewhat close to pkg-config
in idea, but quite different in design. It wraps compiler tools in order to pass options to them.
ocamlbuild
is a fancy swiss-knife that is a must have in the arsenal of every OCamler. It is a tool that knows all other tools, and how to glue them together. I would say that it is the preferred way to compile your projects, especially small one.
oasis
is close to autotools
in the spirit, but not that generic and is written in the premise, that it should be very easy to use. And indeed it is very easy, but still quite flexible and powerful.
With this overview in mind, we can go directly to your problem. So you've installed ctypes. Now let's take a look on how ctypes
package is visible in your system from the ocamlfind
perspective. The easiest way would be 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, that under the ctypes umbrella there're 4 libraries. One basic library, and some extra libraries, that provides some functionality, that is not needed by default.
No let's 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 may see, there is a package
option, to which you can pass a name of the package as found by ocamlfind
, and it will be automagically made visible to the compiler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With