tags:

views:

176

answers:

2

How to Run C Code Block from Erlang? ( Or Call a C function from erlang? )

+5  A: 

The newest approach would consider NIFs http://www.erlang.org/doc/man/erl_nif.html (be careful, it can crash VM). Regular way to do it involves linked in drivers (google up the link, because anti-spam holds it)

+2  A: 

This is for creating a driver

Firstly you'll need to create the C/C++ files to do it.

They will need to include

#include "erl_driver.h"
#include "ei.h"

Then you'll need to set up the driver mapping

/* mapping of the drivers functions */
static ErlDrvEntry driver_entry = {
  NULL,                             /* init */
  startup_function_name,            /* startup  */
  shutdown_function_name,           /* shutdown */
  NULL,                             /* output */
  NULL,                             /* ready_input */
  NULL,                             /* ready_output */
  driver_name,                      /* the name of the driver */
  NULL,                             /* finish */
  NULL,                             /* handle */
  NULL,                             /* control */
  NULL,                             /* timeout */
  outputv_function_name,            /* outputv  */
  NULL,                             /* ready_async */
  NULL,                             /* flush */
  NULL,                             /* call */
  NULL,                             /* event */
  ERL_DRV_EXTENDED_MARKER,          /* ERL_DRV_EXTENDED_MARKER */
  ERL_DRV_EXTENDED_MAJOR_VERSION,   /* ERL_DRV_EXTENDED_MAJOR_VERSION */
  ERL_DRV_EXTENDED_MAJOR_VERSION,   /* ERL_DRV_EXTENDED_MINOR_VERSION */
  ERL_DRV_FLAG_USE_PORT_LOCKING     /* ERL_DRV_FLAGs */
};

DRIVER_INIT(driver_name){
   return &driver_entry;
}

Note: if you are trying to run C++ code instead of C you'll need

extern "C" {
  DRIVER_INIT(driver_name){
    return &driver_entry;
  }
}

And you will need to cast any literal string with (char *)

Then it's good to define a struct that'll contain the port information

typedef struct
{
  ErlDrvPort port;
} port_data;

Lastly, you'll want to set up all the functions

static ErlDrvData startup_function_name(ErlDrvPort port, char *doc)
{
  /* Plus any other start up methods you need */
  port_data* d = (port_data*)driver_alloc(sizeof(port_data));
  d->port = port;
  return (ErlDrvData)d;
}

/* Plus any other shutdown methods you need */
static void shutdown_function_name(ErlDrvData handle)
{
  driver_free((char*)handle);
}

static void outputv_function_name(ErlDrvData handle, ErlIOVec *ev)
{
  port_data* d = (port_data*)handle;
  char* inputstring = ev->binv[1]->orig_bytes;
    ErlDrvTermData spec[] = {
      ERL_DRV_ATOM, driver_mk_atom("ok"),
      ERL_DRV_BUF2BINARY, inputstring, strlen(inputstring)
      ERL_DRV_TUPLE, 2
    };
    driver_send_term(d->port,driver_caller(d->port),spec,sizeof(spec)/sizeof(spec[0]));
}

You'll want to compile this C/C++ code into a shared object and link it with the erl interface

g++ -fpic -rdynamic -shared file_name -lerl_interface -lei

Now from erlang you'll want to do a couple things: You'll need to load the driver

erl_ddll:load_driver("./location/of/driver", driver_name).

Then you'll open a port to the driver

Port = open_port({spawn, driver_name}, [binary]).

And lastly you can sent data to the port

port_command(Port, <<"String to Echo Back"),
receive
  {ok, String} -> io:format("Received ~p back from the driver")
end.
Olives