yallop / ocaml-ctypes

Library for binding to C libraries using pure OCaml
MIT License
363 stars 95 forks source link

exception throw with "libffi does not support passing unions" when parsing union #678

Closed vincentdchan closed 3 years ago

vincentdchan commented 3 years ago

I am trying to call C function with union types, but an error occurs:

Fatal error: exception Ctypes_static.Unsupported("libffi does not support passing unions")
Raised at Ctypes_ffi.Make.report_unpassable in file "src/ctypes-foreign/ctypes_ffi.ml", line 55, characters 4-27
Called from Ctypes_ffi.Make.struct_arg_type.(fun) in file "src/ctypes-foreign/ctypes_ffi.ml", line 84, characters 27-41
Called from Stdlib__list.iteri in file "list.ml", line 114, characters 12-17
Called from Stdlib__list.iteri in file "list.ml" (inlined), line 116, characters 16-27
Called from Ctypes_ffi.Make.struct_arg_type in file "src/ctypes-foreign/ctypes_ffi.ml", line 82, characters 7-193
Called from Ctypes_ffi.Make.prep_callspec in file "src/ctypes-foreign/ctypes_ffi.ml", line 129, characters 24-35
Called from Ctypes_ffi.Make.box_function in file "src/ctypes-foreign/ctypes_ffi.ml", line 136, characters 17-46
Called from Ctypes_ffi.Make.box_function in file "src/ctypes-foreign/ctypes_ffi.ml", line 145, characters 18-45
Called from Ctypes_ffi.Make.pointer_of_function_internal in file "src/ctypes-foreign/ctypes_ffi.ml", line 221, characters 13-36
Called from Ctypes_ffi.Make.pointer_of_function in file "src/ctypes-foreign/ctypes_ffi.ml", line 228, characters 22-101
Called from Ctypes_foreign_basis.Make.funptr in file "src/ctypes-foreign/ctypes_foreign_basis.ml", line 22, characters 16-105
Called from Ctypes_foreign_basis.Make.foreign in file "src/ctypes-foreign/ctypes_foreign_basis.ml", line 44, characters 8-86
Re-raised at Ctypes_foreign_basis.Make.foreign in file "src/ctypes-foreign/ctypes_foreign_basis.ml", line 49, characters 50-59
yallop commented 3 years ago

This is the expected behaviour, unfortunately: it's due to a limitation in the library underlying Foreign (libffi).

You'll need to use stub generation to call C functions that accept unions as arguments.

vincentdchan commented 3 years ago

It's Ok. But it seems that all the tutorials are talking about how to use Foreign. Is there some tutorials about using stub generation?

yallop commented 3 years ago

The blog post Type-safe C bindings #1: Using ocaml-ctypes and stub generation gives a brief introduction. It might also be helpful to look at a project that uses stub generation, such as ocaml-yaml or ocaml-lz4.

vincentdchan commented 3 years ago

Very well, thank you!