3 Problem Example

3.1  Description

A common interoperability situation is when there exists a piece of code solving some complex problem, and we would like to incorporate this piece of code in our Erlang program. Suppose for example we have the following C functions that we would like to be able to call from Erlang.


/* complex.c */

int foo(int x) {
  return x+1;
}

int bar(int y) {
  return y*2;
}

(For the sake of keeping the example as simple as possible, the functions are not very complicated in this case).

Preferably we would like to able to call foo and bar without having to bother about them actually being C functions.

% Erlang code
...
Res = complex:foo(X),
...

The communication with C is hidden in the implementation of complex.erl. In the following chapters it is shown how this module can be implemented using the different interoperability mechanisms.