How code is compiled and loaded is not a language issue, but is system dependent. This chapter describes compilation and code loading in Erlang/OTP with pointers to relevant parts of the documentation.
Erlang programs must be compiled to object code.
The compiler can generate a new file which contains the object
code. The current abstract machine which runs the object code is
called BEAM, therefore the object files get the suffix
.beam
. The compiler can also generate a binary which can
be loaded directly.
The compiler is located in the Kernel module compile
, see
compile(3)
.
compile:file(Module) compile:file(Module, Options)
The Erlang shell understands the command c(Module)
which
both compiles and loads Module
.
There is also a module make
which provides a set of
functions similar to the UNIX type Make functions, see
make(3)
.
The compiler can also be accessed from the OS prompt, see
erl(1)
.
% erl -compile Module1...ModuleN % erl -make
The erlc
program provides an even better way to compile
modules from the shell, see erlc(1)
. It understands a
number of flags that can be used to define macros, add search
paths for include files, and more.
% erlc <flags> File1.erl...FileN.erl
The object code must be loaded into the Erlang runtime
system. This is handled by the code server, see
code(3)
.
The runtime system can be started in either embedded
or
interactive
mode. Which one is determined by the command
line flag -mode
. Default mode is interactive.
In embedded mode, all code is loaded during system start-up
according to the boot file, see OTP System Principles and
script(4)
.
In interactive mode, the code server dynamically loads modules
into the system on demand, which means the first time a function
in a module is called. Initially, the search path consists of
the current working directory and all object code directories
under $ROOT/lib
, where $ROOT
is the installation
directory of Erlang/OTP. The module code
contains
functions for modifying the search path.
Erlang supports change of code in a running system. Code replacement is done on module level.
The code of a module can exist in two variants in a system: current and old. When a module is loaded into the system for the first time, the code becomes 'current'. If then a new instance of the module is loaded, the code of the previous instance becomes 'old' and the new instance becomes 'current'.
Bot old and current code is valid, and may be evaluated concurrently. Fully qualified function calls always refer to current code. Old code may still be evaluated because of processes lingering in the old code.
If a third instance of the module is loaded, the code server will remove (purge) the old code and any processes lingering in it will be terminated. Then the third instance becomes 'current' and the previously current code becomes 'old'.
To change from old code to current code, a process must make a fully qualified function call. Example:
-module(m). -export([loop/0]). loop() -> receive code_switch -> m:loop(); Msg -> ... loop() end.
To make the process change code, send the message
code_switch
to it. The process then will make a fully
qualified call to m:loop()
and change to current code.
Note that m:loop/0
must be exported.