20032009 Ericsson AB. All Rights Reserved. The contents of this file are subject to the Erlang Public License, Version 1.1, (the "License"); you may not use this file except in compliance with the License. You should have received a copy of the Erlang Public License along with this software. If not, it can be retrieved online at http://www.erlang.org/. Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Preprocessor macros.xml
File Inclusion

A file can be included in the following way:

-include(File).
-include_lib(File).

File, a string, should 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 that the file name extension .hrl be used for include files.

File may 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 current working directory, in the same directory as the module being compiled, and in the directories given by the include option, in that order. See erlc(1) and compile(3) for details.

Examples:

-include("my_records.hrl").
-include("incdir/my_records.hrl").
-include("/home/user/proj/my_records.hrl").
-include("$PROJ_ROOT/my_records.hrl").

include_lib is similar to include, but should not 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.

Defining and Using Macros

A macro is defined the following way:

-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 the following way:

?Const ?Func(Arg1,...,ArgN)

Macros are expanded during compilation. A simple macro ?Const will be replaced with Replacement. Example:

-define(TIMEOUT, 200). ... call(Request) -> server:call(refserver, Request, ?TIMEOUT).

This will be expanded to:

call(Request) -> server:call(refserver, Request, 200).

A macro ?Func(Arg1,...,ArgN) will be replaced with Replacement, where all occurrences of a variable Var from the macro definition are replaced with the corresponding argument Arg. Example:

-define(MACRO1(X, Y), {a, X, b, Y}). ... bar(X) -> ?MACRO1(a, b), ?MACRO1(X, 123)

This will be 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 '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:

?MODULE The 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'.
Macros Overloading

This section describes a feature introduced in R13B04. It remains fully compatible with old code but previous versions of erlang does not support the macros overloading.

It is possible to have more than one definition of the same macro, except for predefined macros. Only user-defined macros are considered here. In order to overload a macro, a different signature should be used for each overloaded version. A macro signature consists of its name and its number of arguments.

Object-like macro has no arguments -define(M, io:format("object-like macro")). Function-like macro has 0 or more arguments. -define(M(), io:format("function-like macro with 0 argument")). -define(M(X), io:format("function-like macro with 1 argument: ~p", [X])). -define(M(X, Y), io:format("function-like macro with 2 arguments: (~p, ~p)", [X,Y])).

An object-like macro should not be confused with a function-like macro with 0 argument.

When a macro is used, the preprocessor selects the proper macro following these rules:

if there is only the object-like definition, the preprocessor uses it. Example: -define(FUN, now). ... call() -> {?FUN, ?FUN()}. This will be expended to: call() -> {now, now()}. if the preprocessor recognizes an object-like macro call, and if its definition exists, the preprocessor uses it. Example: -define(MOD, erlang). -define(MOD(X), X). ... call() -> ?MOD:module_info(). This will be expended to: call() -> erlang:module_info(). if the preprocessor recognizes a function-like macro call, the preprocessor chooses the definition with the same number of arguments. Example: -define(CALL(Fun), Fun()). -define(CALL(Mod, Fun), Mod:Fun()). ... call() -> ?CALL(kernel, module_info). This will be expended to: call() -> kernel:module_info(). in all other cases, the preprocessor throws an error. Example: -define(M, erlang). -define(M(X), X). ... call() -> ?M(kernel, module_info). The compiler will fail with error argument mismatch for macro 'M'
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 was false, the lines following else are evaluated instead. -endif. Specifies the end of an ifdef or ifndef directive.

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, debug should be defined when the module m is compiled:

% erlc -Ddebug m.erl

or

1> c(m, {d, debug}).
{ok,m}

?LOG(Arg) will then expand to a call to io:format/2 and provide the user with some simple trace output.

Stringifying Macro Arguments

The construction ??Arg, where Arg is a macro argument, will be expanded to a string containing the tokens of the argument. This is similar to the #arg stringifying construction in C.

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 )",m: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.