How to merge multiple beam files into one beam?

I’m using .beam as script file, then I can hidden my code and run every where.

Usually, there are multiple files work together like:

main.erl
util.erl
....

Is it possible to merge multiple beam files into one beam?

What I can think of is to hardcode the beam into an erl file and then compile it, like

["[",
lists:map(fun(Beam) ->
  {ok, Bin} = prim_file:read_file(Beam),
  ["{", filename:basename(Beam, ".beam"), ",", io_lib:format("~w", [Bin]), "},"]
end, BeamList)
"ok]"]

get

[{module, <<"....">>}]

finally, use code:load_file to load it

I can hidden my code and run every where

.beam files are not entirely portable. Erlang VM cannot load files compiled by newer version of OTP, or too old of a version.

I’m using .beam as script file

Common solution to your problem is escript, a type of an archive that can include multiple beam files, as well as Erlang runtime. Erlang runtime comes with an escript executable that knows how to extract and run these archives. rebar3 supports building escripts.

1 Like

thanks, I will use escript:create/2 to make it :clap: :clap: :clap: