20032018Ericsson AB. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Preprocessormacros.xmlFile Inclusion
A file can be included as follows:
-include(File).
-include_lib(File).
File, a string, is to point out a file. The contents of
this file are included as is, at the position of the directive.
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 .hrl for
include files.
File can start with a path component $VAR, for
some string VAR. If that is the case, the value of
the environment variable VAR as returned by
os:getenv(VAR) is substituted for $VAR. If
os:getenv(VAR) returns false, $VAR is left
as is.
If the filename File is absolute (possibly after
variable substitution), the include file with that name is
included. Otherwise, the specified file is searched for
in the following directories, and in this order:
The current working directoryThe directory where the module is being compiledThe directories given by the include option
For details, see the
erlc(1) manual page
in ERTS and
compile(3)
manual page in Compiler.
include_lib is similar to include, but is not to
point out an absolute file. Instead, the first path component
(possibly after variable substitution) is assumed to be
the name of an application.
Example:
-include_lib("kernel/include/file.hrl").
The code server uses code:lib_dir(kernel) to find
the directory of the current (latest) version of Kernel, and
then the subdirectory include is searched for the file
file.hrl.
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
?Const is replaced with Replacement.
A macro ?Func(Arg1,...,ArgN) is replaced with
Replacement, where all occurrences of a variable Var
from the macro definition are replaced with the corresponding
argument Arg.
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 'P' option. compile:file(File, ['P']).
This produces a listing of the parsed code after preprocessing
and parse transforms, in the file File.P.
Predefined Macros
The following macros are predefined:
?MODULEThe name of the current module.?MODULE_STRING.The name of the current module, as a string.?FILE.The file name of the current module.?LINE.The current line number.?MACHINE.The machine name, 'BEAM'.?FUNCTION_NAMEThe name of the current function.?FUNCTION_ARITYThe arity (number of arguments) for the current function.?OTP_RELEASEThe OTP release that the currently executing ERTS
application is part of, as an integer. For details, see
erlang:system_info(otp_release).
This macro was introduced in OTP release 21.Macros Overloading
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 ?Func(Arg1,...,ArgN) with a (possibly empty)
list of arguments results in an error message if there is at
least one definition of Func with arguments, but none
with N arguments.
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().Flow Control in Macros
The following macro directives are supplied:
-undef(Macro).Causes the macro to behave as if it had never been defined.-ifdef(Macro).Evaluate the following lines only if Macro is
defined.-ifndef(Macro).Evaluate the following lines only if Macro is not
defined.-else.Only allowed after an ifdef or ifndef
directive. If that condition is false, the lines following
else are evaluated instead.-endif.Specifies the end of an ifdef, an ifndef
directive, or the end of an if or elif directive.-if(Condition).Evaluates the following lines only if Condition
evaluates to true.-elif(Condition).Only allowed after an if or another elif directive.
If the preceding if or elif directives do not
evaluate to true, and the Condition evaluates to true,
the lines following the elif are evaluated instead.
The macro directives cannot be used inside functions.
When trace output is desired, debug is to be defined
when the module m is compiled:
% erlc -Ddebug m.erl
or
1> c(m, {d, debug}).
{ok,m}
?LOG(Arg) is then expanded to a call to io:format/2
and provide the user with some simple trace output.
Example:
-module(m)
...
-ifdef(OTP_RELEASE).
%% OTP 21 or higher
-if(?OTP_RELEASE >= 22).
%% Code that will work in OTP 22 or higher
-elif(?OTP_RELEASE >= 21).
%% Code that will work in OTP 21 or higher
-endif.
-else.
%% OTP 20 or lower.
-endif.
...
The code uses the OTP_RELEASE macro to conditionally
select code depending on release.
-error() and -warning() directives
The directive -error(Term) causes a compilation error.
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 -warning(Term) causes a compilation warning.
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 -error() and -warning() directives were added
in OTP 19.
Stringifying Macro Arguments
The construction ??Arg, where Arg is a macro
argument, is expanded to a string containing the tokens of
the argument. This is similar to the #arg stringifying
construction in C.