[ocaml-ctypes] Build help

Jeremy Yallop yallop at gmail.com
Mon Aug 15 12:34:59 BST 2016


Dear Andre,

On 10 August 2016 at 21:20, Andre Nathan <andre at digirati.com.br> wrote:
> I have working ctypes bindings for MariaDB's libmysqlclient at
>
>   https://github.com/andrenth/ocaml-mariadb
>
> I basically copied and adapted the project structure from
>
>   https://github.com/simonjbeaumont/ocaml-flock
>
> to get stub generation working. Currently I'm doing
>
>   let foreign name typ = foreign name typ
>     ~from:Dl.(dlopen ~filename:"libmysqlclient.so" ~flags:[RTLD_NOW])
>
> to avoid getting a build error:

If you're using stub generation (which I recommend), then you
typically don't need to use the Dl module at all.  The usual approach
is to use the 'foreign' function that comes from the argument to your
bindings functor instead of the function from the top-level Foreign
module.  In other words, instead of this:

   module Foreign_bindings = struct
     open Foreign

     let foreign name typ = foreign name typ
       ~from:Dl.(dlopen ~filename:"libmysqlclient.so" ~flags:[RTLD_NOW])

     let mysql_library_init = foreign "mysql_server_init"
       (int @-> ptr_opt (ptr char) @-> ptr_opt (ptr char) @-> returning int)

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'.

Linking is generally much easier with this second approach.  Since
names are resolved statically by the standard C toolchain there's no
need to pass `-Wl,-no-as-needed`, set up LD_LIBRARY_PATH, etc.

Kind regards,

Jeremy.


More information about the Ctypes mailing list