tags:

views:

364

answers:

4

What interfaces exist to tie Erlang with C++?

+7  A: 
  • Native implemented functions: available in the latest Erlang/OTP version, allows you to implement any of your functions in C.

  • Port drivers: you can link a C code to the Erlang VM, and access it using port_command.

  • C Nodes: With the ei library you can mimic a VM and talk to your Erlang VMs using the Erlang distribution format.

Zed
NFI are still experimental... but I am hopeful and eagerly awaiting a stable interface for these!
jldupont
True, they are experimental, but having a "fallback" Erlang function implementation is so nice that it even worth changing your C code for every upcoming release :)
Zed
Of yes, I concur!
jldupont
+1  A: 

The closest thing I know for interfacing Erlang with C++ directly is EPAPI. Of course it relies on the tried and tested C erl_interface that comes standard with the Erlang distribution.

jldupont
EPAPI is at version 0.7 so I presume that one is still experimental as well :o)
Zed
It is very stable nonetheless :-)
jldupont
A: 

To Zed's excellent answer, I would add open_port(). It lets you start an external program and communicate with it from Erlang using its standard in and out. You can use it like Unix pipes by giving the {line, L} option, or there's also the length-prefixed {packet, N} option which I find to be safer and more efficient.

The advantage of this over NIFs and port drivers is that your Erlang code is totally insulated from the C code. The C program can smash its own stack, double-free blocks of memory, enter an infinite loop, whatever. None of this stops your Erlang code. At worst, you close the Erlang port and re-open it if things go pear-shaped.

Warren Young
A: 

Anyone interested in the erlang/C++ integration aspect, may also want to check out this article: Rewriting Playdar: C++ to Erlang, massive savings:

I’ve heard many anecdotes and claims about how many lines of code are saved when you write in Erlang instead of [C++/other language]. I’m happy to report that I now have first-hand experience and some data to share.

I initially wrote Playdar in C++ (using Boost and Asio libraries), starting back in February this year. I was fortunate to be working with some experienced developers who helped me come to terms with C++. There were three of us hacking on it regularly up until a few months ago, and despite being relatively new to C++, I’ll say that we ended up with a well designed and robust codebase, all things considered.

none
They started having written 8,035 lines of code, and a few weeks later they'd written 10,067 lines of code, 80% of which they threw away, had added no new functionality in the process, this is a saving? Must be an open source thing, as there's no way a business would justify that. (writing it in Erlang to start with would be a different matter)
Pete Kirkham