[ocaml-ctypes] Foreign function interface support

Pavel Zaichenkov zaichenkov at gmail.com
Sun Mar 9 01:15:30 GMT 2014


Your suggestions helped me indeed.

I finally made picosat bindings work.
If you are interested, here is the repository:
https://github.com/zayac/ocaml-picosat. This works for OS X only yet,
but I will add relevant flags for Ubuntu later.

On 4 March 2014 11:34, Jeremy Yallop <yallop at gmail.com> wrote:
> 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