[ocaml-ctypes] Build help

Jeremy Yallop yallop at gmail.com
Tue Aug 16 07:28:29 BST 2016


On 15 August 2016 at 21:41, Andre Nathan <andre at digirati.com.br> wrote:
> On 08/15/2016 08:34 AM, Jeremy Yallop wrote:
>> you'd write something like this:
>>
>>    module Foreign_bindings(F: Cstubs.FOREIGN) = struct
>>      open F
>>
>>      let mysql_library_init = foreign "mysql_server_init"
>>        (int @-> ptr_opt (ptr char) @-> ptr_opt (ptr char) @-> returning int)
>>
>> In this second snippet 'foreign' refers to 'F.foreign', not 'Foreign.foreign'.
>
> In this case a binding that had type "a -> b" now became
>
>   (a -> b F.return) F.result
>
> For example, this binding:
>
>   let mysql_init = foreign "mysql_init"
>     (T.mysql_opt @-> returning T.mysql_opt)
>
> had type "T.mysql_opt -> T.mysql_opt" but after the change it became
>
>   (T.mysql_opt -> T.mysql_opt F.return) F.result
>
> This way I can't use it as a function anymore. Is this expected?

It's expected, but it's not the problem that it initially appears to be.

*Inside* the functor the binding has non-function type: it's not
available for calling at that point, since it hasn't yet been linked
together with the generated code.

*Outside* the functor, once you've applied the functor to the module
that ctypes generates, the function becomes available for calling.

For example, here's the bindings functor from the unix-dirent package,
which binds opendir, readdir, etc.:

    https://github.com/dsheets/ocaml-unix-dirent/blob/71590346/lib_gen/unix_dirent_bindings.ml#L40-L56

(Note that the functor contains only bindings for functions, not calls
to those bindings.)

And here's the line of code that links together the bindings functor
with the generated module:

    https://github.com/dsheets/ocaml-unix-dirent/blob/71590346/unix/dirent_unix.ml#L19

A little further down, the code makes calls to the bindings:

    https://github.com/dsheets/ocaml-unix-dirent/blob/71590346/unix/dirent_unix.ml#L39

Kind regards,

Jeremy.


More information about the Ctypes mailing list