Skip to content
yallop edited this page May 20, 2014 · 18 revisions

Building and installing ctypes

Using ctypes

Support and documentation

Questions about C types

Pointers, strings and arrays

Calling functions

Function pointers

Structs and unions

Enums

Memory management

Miscellaneous questions


Building and installing ctypes

How can I install ctypes?

How should I install libffi on Mac OS X?

How can I build ctypes with debug support?

Does ctypes work on Windows?

Using ctypes

How can I print ctypes values in the toplevel?

Use the ctypes.top package. Loading the package installs printers for various ctypes values. You can load the package using Findlib's #require directive:

# #use "topfind";;
- : unit = ()
Findlib has been successfully loaded. Additional directives:
  #require "package";;      to load a package
  #list;;                   to list the available packages
  #camlp4o;;                to load camlp4 (standard syntax)
  #camlp4r;;                to load camlp4 (revised syntax)
  #predicates "p,q,...";;   to set these predicates
  Topfind.reset();;         to force that packages will be reloaded
  #thread;;                 to enable threads

- : unit = ()
# #require "ctypes.top";;
$HOME/.opam/4.01.0/lib/ocaml/unix.cma: loaded
$HOME/.opam/4.01.0/lib/ocaml/bigarray.cma: loaded
$HOME/.opam/4.01.0/lib/ocaml/str.cma: loaded
$HOME/.opam/4.01.0/lib/ctypes: added to search path
$HOME/.opam/4.01.0/lib/ctypes/ctypes.cma: loaded
$HOME/.opam/4.01.0/lib/ctypes/ctypes-top.cma: loaded
# open Ctypes;;

Once the package is loaded, the results of expressions that evaluate to C type descriptions are printed using C declaration syntax. For example, the type "array of 10 floats" is printed as float[10]:

# array 10 float;;
- : float carray typ = float[10]

and the type "function accepting two ints and returning a pointer to an array of 10 chars" is printed as follows:

# int @-> int @-> returning (ptr (array 10 char));;
- : (int -> int -> char carray ptr) fn = char(*(int, int))[10]

C values are also printed out, in a form similar to C's initializer syntax. Here's how ctypes prints an array of three floats:

# CArray.of_list float [1.0; 2.0; 3.0];;
- : float carray = { 1, 2, 3 }

What's the build process for code that uses stub generation ?

Why does stub generation use functors?

Does stub generation support the "noalloc" and "float" flags?

What's the purpose of the -Wl,-no-as-needed flag?

Support and documentation

What ctypes documentation is available?

Where can I get support for ctypes?

There's a mailing list hosted on lists.ocaml.org and mirrored on gmane:

If you find a bug, or would like to propose or contribute a new feature, please visit the GitHub issue tracker

Who is using ctypes?

Questions about C types

Pointers, strings and arrays

What's the best way to bind C functions that act on strings?

How can I deal with null pointers?

Does ctypes support bigarrays?

Calling functions

How can I call variadic functions?

How can I call C macros?

How can I call compiler builtins?

How can I bind to functions that set errno?

Function pointers

What is the lifetime of an OCaml function passed to C?

How can I pass OCaml functions to C?

Structs and unions

How can I declare an incomplete type?

How can I represent structures with bit-field members?

How does ctypes handle struct layout?

How can I represent packed structs?

How can represent structs that use the "struct hack"?

Enums

How can I represent enums?

Memory management

How can I attach finalisers to objects created by ctypes?

When should I use finalisers?

What do I need to know about the garbage collector?

When does ctypes copy objects?

Miscellaneous questions

What's the purpose of views?

How can I cast between types?

How can I access global C variables?

How can I bind to C++ code?

How can I expose OCaml code to C?

How can I improve the performance of my ctypes code?

Clone this wiki locally