I have to write an Erlang driver so I started by triying a little one : driverc.c
, the code is simple, the Driver Erlang Data is the address of a long number and each call to the driver is to increment this number by n
or multipliying this number by n
, n
is just one digit number (0,…, 9) and is passed as char
, the code is:
#include<stdio.h>
#include<stdlib.h>
#include<erl_driver.h>
#include<ei.h>
#include<string.h>
#define SOMME 1
#define PRODUIT 2
static ErlDrvData start(ErlDrvPort port, char *command) ;
static void stop(ErlDrvData data) ;
static ErlDrvSSizeT control(ErlDrvData data, unsigned int command, char *buf, ErlDrvSizeT len, char **rbuf, ErlDrvSizeT rlen);
static long *number;
static ErlDrvEntry myentry={
NULL,
start,
stop,
NULL,
NULL,
NULL,
"driverc",
NULL,
NULL,
control,
NULL,
NULL,
NULL,
NULL,
NULL
};
DRIVER_INIT(driverc)
{return &myentry;}
ErlDrvData start(ErlDrvPort port, char *command)
{number=(long*)driver_alloc(sizeof(long)) ;
*number=0;
set_port_control_flags(port,PORT_CONTROL_FLAG_BINARY);
return (ErlDrvData)number;
}
void stop(ErlDrvData data)
{long *olddata;
olddata=(long*)data;
driver_free(olddata);
}
ErlDrvSSizeT control(ErlDrvData data, unsigned int command, char *buf, ErlDrvSizeT len, char ** rbuf, ErlDrvSizeT rlen)
{long reply, m, *olddata, newdata;
int index, result, n;
char c;
c=*buf;
n=c-'0'; /* convert character representation to true integer digit */
m=(long)n;
olddata=(long*)data;
index=0;
switch (command) {
case SOMME : newdata=(*olddata)+m;
*olddata=newdata;
reply=newdata;
break;
case PRODUIT : newdata=(*olddata)*m;
*olddata=newdata;
reply=newdata;
break;
default : reply=-1;
break;
}
result=ei_encode_long(*rbuf,&index,reply) ;
/* this function convert a long number (reply) to binary and write it in *rbuf */
return sizeof(reply) ;
}
When I tried to compile it, it returns errors that I can’t know where is the problem, the shell output is below.
cc driverc.c -I/usr/local/lib/erlang/usr/include
error : undefined symbol : main
referenced by ....
error : undefined symbol : driver_alloc
referenced by ....
error : undefined symbol : set_port_control_flags
referenced by ....
error : undefined symbol : ei_encode_long
referenced by ....
error : linked command failed with exit code 1 ....