[ocaml-ctypes] Foreign function interface support

Jeremy Yallop yallop at gmail.com
Tue Mar 4 11:34:51 GMT 2014


On 04/03/2014, Pavel Zaichenkov <zaichenkov at gmail.com> wrote:
> It fails for both OS X and Ubuntu.

Passing -Wl,--no-as-needed should fix the problem on Ubuntu.  Here's a
full example that works (for me) on both Ubuntu and OS X.  I'm calling
ocamlfind directly rather than using corebuild, but perhaps this'll be
some help in constructing a corebuild command that does what you need.
 (The '-verbose 1' corebuild option will show you the commands that
corebuild is executing.)

In both cases picosat.ml and test.ml contain exactly the code that you posted.

On Ubuntu:

  $ ocamlfind opt -a -cclib -lpicosat -cclib -Wl,--no-as-needed
-package ctypes.foreign picosat.ml -o picosat.cmxa
  $ ocamlfind opt    -cclib -lpicosat -cclib -Wl,--no-as-needed
-package ctypes.foreign -linkpkg picosat.cmxa test.ml -o test.native

On OS X

  $ ocamlfind opt -a -cclib -lpicosat -package ctypes.foreign
picosat.ml -o picosat.cmxa
  $ ocamlfind opt    -cclib -lpicosat -package ctypes.foreign -linkpkg
picosat.cmxa test.ml -o test.native

You can check that the picosat library has been linked in using 'ldd
test.native' on Ubuntu or 'otool -L test.native' on OS X; you should
see a line like this:

   libpicosat.so.1 => /usr/lib/libpicosat.so.1 (0x00007fa11567d000)

or like this:

   libpicosat.dylib (compatibility version 0.0.0, current version 0.0.0)

> Btw, I get "Unbound value returning_checking_errno" error for datetime
> example in both systems.

As Daniel said, this is due to an API change in ctypes between 0.1.X
(which is described in RWO) and 0.2.X (which is what OPAM now installs
by default).  It's still possible to install 0.1.1 using OPAM if you
want a version of ctypes that's fully compatible with the RWO text,
but the API changes are actually fairly few, so it may be worth
persisting with the latest version.  In this case the change you need
is to change

   foreign "gettimeofday"
      (ptr timeval @-> ptr timezone @-> returning_checking_errno int)

to

   foreign "gettimeofday" ~check_errno:true
      (ptr timeval @-> ptr timezone @-> returning int)

Hope that helps,

Jeremy.


More information about the Ctypes mailing list