【问题标题】:Erlang Driver ErrorsErlang 驱动程序错误
【发布时间】:2021-12-25 23:54:02
【问题描述】:

我要写一个 Erlang 驱动,所以我先尝试了一点:driverc.c,代码很简单,Driver Erlang Data 是一个长数字的地址,每次调用驱动都会增加这个数字通过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) ;
} 

当我尝试编译它时,它返回错误,我不知道问题出在哪里,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

【问题讨论】:

  • 旁白:我的 AV 已阻止链接。请在问题中以文字形式发布。
  • 是的,我当然会编辑它

标签: c erlang freebsd erlang-otp


【解决方案1】:

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

驱动程序将被编译并链接到共享库(Windows 上的 DLL)。使用 gcc,这是通过链接标志 -shared-fpic 完成的。当我们使用ei 库时,我们也应该包含它。

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

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

【讨论】:

  • 谢谢legoscia,我把它贴在Erlang论坛上,昨天已经按照你说的回答了,但问题是为什么使用这些标志可以解决错误?让我们考虑这是一个具有main函数的“最终”C程序,依赖于main的第一个错误应该是resolvedrd,但依赖于包含的头文件(erl_driver.hei.h)的其他错误将直到使用这两个标志才解决,为什么?
猜你喜欢
  • 2016-12-06
  • 2015-09-03
  • 1970-01-01
  • 2022-01-28
  • 2013-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-12
相关资源
最近更新 更多