在 Erlang 中运行 C 代码块

发布于 2024-09-15 16:14:59 字数 49 浏览 3 评论 0原文

如何从 Erlang 运行 C 代码块? (或者从 erlang 调用 C 函数?)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千紇 2024-09-22 16:14:59

这是为了创建驱动程序。

首先,您需要创建 C/C++ 文件来执行此操作。

他们将需要包含

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

然后您需要设置驱动程序映射

/* 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;
}

注意:如果您尝试运行 C++ 代码而不是 C,您将需要

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

并且您将需要使用 (char *) 转换任何文字字符串

那么它很好定义一个包含端口信息的结构体

typedef struct
{
  ErlDrvPort port;
} port_data;

最后,您需要设置所有函数

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]));
}

您需要将此 C/C++ 代码编译为共享对象并将其与 erl 接口链接

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

现在从 erlang 您'我想做几件事:
您需要加载驱动程序

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

然后您将打开驱动程序的端口

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

最后您可以将数据发送到该端口

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

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.
梦屿孤独相伴 2024-09-22 16:14:59

最新的方法将考虑 NIF http://www.erlang.org/doc/man/erl_nif .html(小心,它可能会使虚拟机崩溃)。常规方法涉及链接到驱动程序(谷歌搜索链接,因为反垃圾邮件保留它)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文