在二郎山运行C code座 [英] Run C Code Block in Erlang

查看:147
本文介绍了在二郎山运行C code座的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从二郎运行C code座? (或呼叫的Erlang C函数?)

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

推荐答案

这是创建驱动程序

首先,你需要创建的C / C ++文件来做到这一点。

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

他们将需要包括

#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;
}

请注意:如果你想运行C ++ code而不是C的你需要

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

您会想编译这个C / C ++ code到一个共享对象,并与ERL接口链接它

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.

这篇关于在二郎山运行C code座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆