Chapter outline
This chapter introduces the tools that allow interoperability between
Objective CAML and C by building executables containing code fragments
written in both languages. These tools include functions to convert
between the data representations of each language, allocation functions
using the Objective CAML heap and garbage collector, and functions to raise
Objective CAML exceptions from C.
The first section shows how to call C functions from Objective CAML and how
to build executables and interactive toplevel interpreters including the C code
implementing those functions. The second section explores the C
representation of Objective CAML values. The third section explains how to
create and modify Objective CAML values from C. It discusses the
interactions between C allocations and the Objective CAML garbage collector,
and presents the mechanisms ensuring safe allocation from C. The
fourth section describes exception handling: how to raise exceptions
and how to handle them. The fifth section reverses the roles: it
shows how to include Objective CAML code in an application whose main program is written in C.
Note
This chapter assumes a working knowledge of the C language. Moreover,
reading chapter 9 can be helpful in understanding the issues raised by
automatic memory management.