Erlang 驱动程序错误

Erlang Driver Errors

我必须写一个 Erlang 驱动程序所以我开始尝试一个小的:driverc.c,代码很简单,驱动程序 Erlang 数据是一个长数字的地址,每次调用驱动程序是将此数字增加 n 或将此数字乘以 nn 只是一位数字 (0,...., 9) 并作为 char 传递,代码为:

#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, insigned int command, char *buf, ErlDrvSizeT len, char **buf, 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) ;
} 

当我尝试编译它时,出现 returns 错误,我不知道问题出在哪里,shell 输出如下。

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

https://ibb.co/LnTDtmt

来自“编译和链接示例驱动程序”的 documentation

The driver is to be compiled and linked to a shared library (DLL on Windows). With gcc, this is done with link flags -shared and -fpic. As we use the ei library, we should include it too.

链接器抱怨找不到 main,因为它认为它正在构建一个独立的程序。将其构建为共享库应该可以解决这个问题。

Erlang/OTP 存储库中有 a sample Makefile 可能有用 - 它包含所有必需的标志。