A file can be included as follows:
-include(File). -include_lib(File).
Include files are typically used for record and macro
definitions that are shared by several modules. It is
recommended to use the file name extension
If the filename
For details, see the
Examples:
-include("my_records.hrl"). -include("incdir/my_records.hrl"). -include("/home/user/proj/my_records.hrl"). -include("$PROJ_ROOT/my_records.hrl").
Example:
-include_lib("kernel/include/file.hrl").
The code server uses
A macro is defined as follows:
-define(Const, Replacement).
-define(Func(Var1,...,VarN), Replacement).
A macro definition can be placed anywhere among the attributes and function declarations of a module, but the definition must come before any usage of the macro.
If a macro is used in several modules, it is recommended that the macro definition is placed in an include file.
A macro is used as follows:
?Const
?Func(Arg1,...,ArgN)
Macros are expanded during compilation. A simple macro
Example:
-define(TIMEOUT, 200).
...
call(Request) ->
server:call(refserver, Request, ?TIMEOUT).
This is expanded to:
call(Request) ->
server:call(refserver, Request, 200).
A macro
Example:
-define(MACRO1(X, Y), {a, X, b, Y}).
...
bar(X) ->
?MACRO1(a, b),
?MACRO1(X, 123)
This is expanded to:
bar(X) ->
{a,a,b,b},
{a,X,b,123}.
It is good programming practice, but not mandatory, to ensure that a macro definition is a valid Erlang syntactic form.
To view the result of macro expansion, a module can be compiled
with the
The following macros are predefined:
It is possible to overload macros, except for predefined macros. An overloaded macro has more than one definition, each with a different number of arguments.
The feature was added in Erlang 5.7.5/OTP R13B04.
A macro
Assuming these definitions:
-define(F0(), c).
-define(F1(A), A).
-define(C, m:f).
the following does not work:
f0() ->
?F0. % No, an empty list of arguments expected.
f1(A) ->
?F1(A, A). % No, exactly one argument expected.
On the other hand,
f() ->
?C().
is expanded to
f() ->
m:f().
The following macro directives are supplied:
The macro directives cannot be used inside functions.
Example:
-module(m).
...
-ifdef(debug).
-define(LOG(X), io:format("{~p,~p}: ~p~n", [?MODULE,?LINE,X])).
-else.
-define(LOG(X), true).
-endif.
...
When trace output is desired,
% erlc -Ddebug m.erl or 1> c(m, {d, debug}). {ok,m}
The directive
Example:
-module(t).
-export([version/0]).
-ifdef(VERSION).
version() -> ?VERSION.
-else.
-error("Macro VERSION must be defined.").
version() -> "".
-endif.
The error message will look like this:
% erlc t.erl t.erl:7: -error("Macro VERSION must be defined.").
The directive
Example:
-module(t).
-export([version/0]).
-ifndef(VERSION).
-warning("Macro VERSION not defined -- using default version.").
-define(VERSION, "0").
-endif.
version() -> ?VERSION.
The warning message will look like this:
% erlc t.erl t.erl:5: Warning: -warning("Macro VERSION not defined -- using default version.").
The
The construction
The feature was added in Erlang 5.0/OTP R7.
Example:
-define(TESTCALL(Call), io:format("Call ~s: ~w~n", [??Call, Call])).
?TESTCALL(myfunction(1,2)),
?TESTCALL(you:function(2,1)).
results in
io:format("Call ~s: ~w~n",["myfunction ( 1 , 2 )",myfunction(1,2)]),
io:format("Call ~s: ~w~n",["you : function ( 2 , 1 )",you:function(2,1)]).
That is, a trace output, with both the function called and the resulting value.