Take the 2-minute tour ×

Francesco Cesarini's "Erlang Programming" book provides a nice and simple-to-start-with example of connecting Erlang to Ruby (implemented through ports):


test() ->
    Cmd = "ruby echoFac.rb",
    Port = open_port({spawn, Cmd}, [{packet, 4}, use_stdio, exit_status, binary]),
    Payload = term_to_binary({fac, list_to_binary(integer_to_list(23))}),
    port_command(Port, Payload),
     {Port, {data, Data}} ->
      {result, Text} = binary_to_term(Data),
      Blah = binary_to_list(Text),
      io:format("~p~n", [Blah])

However, the Ruby code used in this example uses the Erlictricity library, which does all the low-level things for the programmer:

require 'rubygems'
require 'erlectricity'
require 'stringio'
def fac n
if (n<=0) then 1 else n*(fac (n-1)) end
receive do |f|
f.when(:fac, String) do |text|
n = text.to_i
f.send!(:result, "#{n}!=#{(fac n)}")

I have tried to use this slightly modified test.erl code:

test(Param) ->
        Cmd = "./add",
        Payload = term_to_binary({main, list_to_binary(integer_to_list(Param))}),

to speak with a very simple C file:

/* add.c */
#include <stdio.h>
int main(int x) {
 // return x+1;

But unfortunately the receive loop in test.erl gets a message {#Port<0.2028>,{exit_status,2}}

My question is: is it possible to implement anything similar in C/C++? Are there any ready-made libraries for Erlang to talk with C/C++ via ports similar to Erlictricity for Ruby?

share|improve this question
A crucial point is what sort of data you want to pass between Erlang and C. Just strings (or binary blobs in general), integers or floating-point numbers? Or do you want to send structured data, like arbitrary Erlang tuples, to the C side? And what should the C code return to Erlang - just a status code, a string, integer or float, or is it supposed to produce some compound data structure? –  RichardC Oct 8 '11 at 16:27
It can be lists of integers and floats or lists of tuples (holding integer and float elements) that I want to pass from Erlang to C. C side should return lists of integers or lists of tuples with integer elements. –  Martin Lee Oct 8 '11 at 21:31
In that case, you don't really need the help (and overhead) of any library. Just design a very simple protocol and send raw bytes back and forth. –  RichardC Oct 8 '11 at 23:04
Dear RichardC, thanks! Unfortunately my knowledge of communication between languages on Unix (and on any OS actually) is very limited. Could you please reference me somewhere where I can read about this language-to-language communication? The only two things that I know now is that (a) with an ad-hoc protocol I define the form of the passed data on the both sides and (b) I could probably call shell commands from Erlang with stringified parameters (like "./add X", where X comes from an Erlang variable). –  Martin Lee Oct 9 '11 at 19:10
RichardC, could you please post your last comment as an answer? I will mark it as accepted. –  Martin Lee Oct 12 '11 at 9:03
add comment

2 Answers

up vote 1 down vote accepted

Start by reading the Interoperability Tutorial in the Erlang/OTP online documentation: http://erlang.org/doc/tutorial/users_guide.html. When communicating with a C program, you just write the C code to read from stdin and write to stdout, and this will be hooked up to the Erlang port. You could also read chapter 12 in http://manning.com/logan.

share|improve this answer
add comment

Have you checked out Erl Interface here: http://www.erlang.org/doc/tutorial/erl_interface.html ?
Other interesting links i have found are listed below:


I hope those will help :)

share|improve this answer
I have read it and I have to admit that I was unable to make the example work (on the other side the Ruby example is much simpler). If it is the only option, then I have to devote it more time :) –  Martin Lee Oct 8 '11 at 12:29
add comment

Your Answer


