2
votes

I am trying to use in Erlang module a beam-file compiled from Elixir source. It raises error when I run Erlang node but I can use the code directly from Elixir.

Elixir module:

defmodule Recursion do
  def print_multiple_times(msg, n) when n <= 1 do
    IO.puts msg
  end

  def print_multiple_times(msg, n) do
    IO.puts msg
    print_multiple_times(msg, n - 1)
  end
end

Erlang module:

-module(use_recur).
-define(elixir__recursion, 'Elixir.Recursion').

-export([hey/0]).

hey() ->
    ?elixir__recursion:print_multiple_times("Hello!", 3).

Compile both:

$ rm -f *.beam $ elixirc recursion.ex
$ erlc use_recur.erl

Run Erlang:

$ erl -run use_recur hey -run init stop -noshell {"init terminating in do_boot",{undef,[{'Elixir.IO',puts,["Hello!"],[]},{'Elixir.Recursion',print_multiple_times,2,[{file,"recursion.ex"},{line,7}]},{init,start_em,1,[]},{init,do_boot,3,[]}]}} init terminating in do_boot ({undef,[{Elixir.IO,puts,Hello!,[]},{Elixir.Recursion,print_multiple_times,2,[{},{}]},{init,start_em,1,[]},{init,do_boot,3,[]}]})

Crash dump is being written to: erl_crash.dump...done

Elixir script:

Recursion.print_multiple_times "Hello!", 3

Runs successfully:

$ elixir elx_recur.exs  
Hello!
Hello!
Hello!

Why does it happen? I'd say Erlang's output should be the same.

1
You probably need to add the directory containing beam files for Elixir to erl using -pa (e.g. erl -pa /path/to/elixir/ebin ...). The error means Erlang could not find the 'Elixir.IO' module.Dogbert
Alexanders-MacBook-Air:~ alexander$ erl -pa /usr/local/Cellar/elixir/1.4.5/lib/elixir/ebin -run use_recur hey -run init stop -noshell Hello! Hello! Hello! Alexanders-MacBook-Air:~ alexander$ ls /usr/local/Cellar/elixir/1.4.5/lib/elixir/ebin/ | grep Elixir.String.beam Elixir.String.beamperoksid
Can you repost the comment as an answer so I could mark it?peroksid

1 Answers

3
votes

The error means that Erlang could not find a module named 'Elixir.IO'. This module is part of core Elixir. You'll need to add the ebin folder of your Elixir installation to Erlang's load path using -pa (or other similar flags like -pz) to make Erlang load Elixir's core libraries, as that folder contains the compiled .beam files of Elixir core, including Elixir.IO.beam.

erl -pa /path/to/elixir/ebin ...