English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Erlang Header Files

Header files are similar to include files in any other programming language. It is useful to split modules into different files and then access these header files in different programs. Let's see an example of a running header file to understand this.

First, create a file nameduser.hrlin the file, and add the following code-

-record(person, {name = "", id}).

Now, add the following code to our main program file-

Online Examples

-module(helloworld). 
-export([start/0]). 
-include("user.hrl"). 
start() -> 
   P = #person{name = "John", id = 1 
   io:fwrite("~p~n",[P#person.id]), 
   io:fwrite("~p~n",[P#person.name]).

As you can see in the program above, we actually just included the user.hrl file, which will automatically insert-record code.

If the above program is executed, the following output will be obtained.

1
"John"

You can also perform the same operation on macros, where you can define macros in header files and refer to them in the main file. Let's look at an example-

First, create a file nameduser.hrlin the file, and add the following code-

-define(macro1(X,Y),{X+Y}).

Now, add the following code to our main program file-

Online Examples

-module(helloworld). 
-export([start/0]). 
-include("user.hrl"). 
start() -> 
   io:fwrite("~w",[?macro1(1,2])]).

If the above program is executed, the following output will be obtained-

Output Result

{3}