`ctypes-foreign` library not found, despite `opam install ctypes-foreign`

Describing here an issue I stumbled upon, the solution, and some tips hoping it will save time to someone else.

Following the instructions on Real-World OCaml chapter Foreign Function Interface, I installed the required packages (opam install ctypes ctypes-foreign), then added the dependency in the dune-file:

(executable
    …
    (libraries ctypes-foreign))

but got told by dune that the ctypes-foreign library could not be found and consequently the open Foreign instruction failed.

How to fix this?

1 Like

Short answer: the package name ctype-foreign does not imply the library name (packages can contain multiple libraries); the library name to use in the dune-file is ctypes.foreign.

I have the following versions installed (I don’t know if future versions may differ):

$ opam list 'ctype*'
# Packages matching: (installed | available) & name-match(ctype*)
# Name          # Installed # Synopsis
ctypes          0.20.1      Combinators for binding to C libraries without writing any C
ctypes-foreign  0.18.0      Virtual package for enabling the ctypes.foreign subpackage.

How did I figure out the name?

  • a hint came from a GitHub issue of the Real-World Ocaml repository where a variant of the library is mentioned:

    If instead I use ctypes.foreign.threaded it works fine.

  • a confirmation came from this ctype tutorial, showing how to load the library in the toplevel (utop) session:

    #require "ctypes.foreign";;

I don’t know if there is a way to list all names of libraries exported by a package.

I tried opam show <package> to see if that could be a reliable way to know what libraries a package included, but the output does not contain an exported libraries field.
The synopsis of ctypes-foreign says “Virtual package for enabling the ctypes.foreign subpackage”, which hints at the right name, but is also inconsistent with dune’s DSL: subpackage = library?

2 Likes

Did you use the word library, or libraries? The former is for declaring a new library, the latter is for declaring an existing library as a dependency. So in your case the latter would be needed, right?

Also if RWO has a typo, perhaps you can file a bug report on the repo? I’m sure it would be better to fix the typo than document it :wink:

Use:

$ ocamlfind -list
...
ctypes              (version: 0.19.1)
ctypes.foreign      (version: 0.19.1)
ctypes.stubs        (version: 0.19.1)
ctypes.top          (version: 0.19.1)
...

assuming the typical situation where your switch already has ocamlfind (opam install ocamlfind).

2 Likes

I meant to write libraries in my post, my typo here, now fixed.

I thought there may be a typo in the book but I think there isn’t. The book contains (libraries ctypes-foreign). I thought there may be a typo in ctypes-foreign as the library name is not what I get following the book instructions, but the book is in continuous integration, apparently. The README invites a make test to check all the code is correct. In that case the code is correct according to their build environment.

In fact, I just noticed on the book README:

We also use a pinned version of ctypes until the dune-port is stable. To set these up, you can run:

opam repository add dune-opam-overlays git+https://github.com/dune-universe/opam-overlays.git
opam pin add ctypes.0.20.1+dune https://github.com/avsm/ocaml-ctypes.git#dune-port
opam pin add ctypes-foreign.0.20.1+dune https://github.com/avsm/ocaml-ctypes.git#dune-port

I’m thinking that rather than a typo here, it’s a matter of misleading, or incomplete, setup instructions. Makes sense they have a reproducible environment to test the code, I don’t understand why such instructions are not part of the “Installation Instructions” chapter. I’m not complaining about this great and gratis resouce, but can be confusing and frustrating for those approaching OCaml.

The problem has also been noted on the GitHub issue:

I tried to fix the book but then the tests fail. I suspect it’s because the ctypes version in the duniverse folder is out of date?

Also the last comment (Jul 2021) on that issue says:

[…] it can be misleading if one were following the example from scratch instead of cloning directly the book.

So it seems the problem has been signalled.

2 Likes