1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.3 -->
<HTML>
<HEAD>
<TITLE>Compilation and Code Loading</TITLE>
<SCRIPT type="text/javascript" src="../../doc/erlresolvelinks.js">
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
ALINK="#FF0000">
<CENTER>
<A HREF="http://www.erlang.se"><IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif"></A>
</CENTER>
<A NAME="12"><!-- Empty --></A>
<H2>12 Compilation and Code Loading</H2>
<P>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.
<A NAME="12.1"><!-- Empty --></A>
<H3>12.1 Compilation</H3>
<P>Erlang programs must be <STRONG>compiled</STRONG> 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
<CODE>.beam</CODE>. The compiler can also generate a binary which can
be loaded directly.
<P>The compiler is located in the Kernel module <CODE>compile</CODE>, see
<CODE>compile(3)</CODE>.
<PRE>
compile:file(Module)
compile:file(Module, Options)
</PRE>
<P>The Erlang shell understands the command <CODE>c(Module)</CODE> which
both compiles and loads <CODE>Module</CODE>.
<P>There is also a module <CODE>make</CODE> which provides a set of
functions similar to the UNIX type Make functions, see
<CODE>make(3)</CODE>.
<P>The compiler can also be accessed from the OS prompt, see
<CODE>erl(1)</CODE>.
<PRE>
% erl -compile <STRONG>Module1</STRONG>...<STRONG>ModuleN</STRONG>
% erl -make
</PRE>
<P>The <CODE>erlc</CODE> program provides an even better way to compile
modules from the shell, see <CODE>erlc(1)</CODE>. It understands a
number of flags that can be used to define macros, add search
paths for include files, and more.
<PRE>
% erlc <STRONG><flags></STRONG> <STRONG>File1.erl</STRONG>...<STRONG>FileN.erl</STRONG>
</PRE>
<A NAME="loading"><!-- Empty --></A><A NAME="12.2"><!-- Empty --></A>
<H3>12.2 Code Loading</H3>
<P>The object code must be <STRONG>loaded</STRONG> into the Erlang runtime
system. This is handled by the <STRONG>code server</STRONG>, see
<CODE>code(3)</CODE>.
<P>The code server loads code according to a code loading strategy
which is either <STRONG>interactive</STRONG> (default) or
<STRONG>embedded</STRONG>. In interactive mode, code are searched for in
a <STRONG>code path</STRONG> and loaded when first referenced. In
embedded mode, code is loaded at start-up according to a <STRONG>boot
script</STRONG>. This is described in <STRONG>System Principles</STRONG>.
<A NAME="12.3"><!-- Empty --></A>
<H3>12.3 Code Replacement</H3>
<P>Erlang supports change of code in a running system. Code
replacement is done on module level.
<P>The code of a module can exist in two variants in a system:
<STRONG>current</STRONG> and <STRONG>old</STRONG>. 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'.
<P>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.
<P>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'.
<P>To change from old code to current code, a process must make a
fully qualified function call. Example:
<PRE>
-module(m).
-export([loop/0]).
loop() ->
receive
code_switch ->
m:loop();
Msg ->
...
loop()
end.
</PRE>
<P>To make the process change code, send the message
<CODE>code_switch</CODE> to it. The process then will make a fully
qualified call to <CODE>m:loop()</CODE> and change to current code.
Note that <CODE>m:loop/0</CODE> must be exported.
<P>For code replacement of funs to work, the tuple syntax
<CODE>{Module,FunctionName}</CODE> must be used to represent the fun.
<CENTER>
<HR>
<SMALL>
Copyright © 1991-2006
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>
|