User's Guide
PDF
Top

Interoperability Tutorial
User's Guide
Version 5.8.5

Expand All
Contract All

Chapters

- Introduction
  - Top of chapter
  - Purpose
  - Prerequisites
- Overview
  - Top of chapter
  - Built-In Mechanisms
  - C and Java Libraries
  - Standard Protocols
  - IC
  - Old Applications
- Problem Example
  - Top of chapter
  - Description
- Ports
  - Top of chapter
  - Erlang Program
  - C Program
  - Running the Example
- Erl_Interface
  - Top of chapter
  - Erlang Program
  - C Program
  - Running the Example
- Port drivers
  - Top of chapter
  - Port Drivers
  - Erlang Program
  - C Driver
  - Running the Example
- C Nodes
  - Top of chapter
  - Erlang Program
  - C Program
  - Running the Example
- NIFs
  - Top of chapter
  - NIFs
  - Erlang Program
  - NIF library code
  - Running the Example

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