
- Erlang - Home
- Erlang - Overview
- Erlang - Environment
- Erlang - Basic Syntax
- Erlang - Shell
- Erlang - Data Types
- Erlang - Variables
- Erlang - Operators
- Erlang - Loops
- Erlang - Decision Making
- Erlang - Functions
- Erlang - Modules
- Erlang - Recursion
- Erlang - Numbers
- Erlang - Strings
- Erlang - Lists
- Erlang - File I/O
- Erlang - Atoms
- Erlang - Maps
- Erlang - Tuples
- Erlang - Records
- Erlang - Exceptions
- Erlang - Macros
- Erlang - Header Files
- Erlang - Preprocessors
- Erlang - Pattern Matching
- Erlang - Guards
- Erlang - BIFS
- Erlang - Binaries
- Erlang - Funs
- Erlang - Processes
- Erlang - Emails
- Erlang - Databases
- Erlang - Ports
- Erlang - Distributed Programming
- Erlang - OTP
- Erlang - Concurrency
- Erlang - Performance
- Erlang - Drivers
- Erlang - Web Programming
Erlang - Header Files
Header files are like include files in any other programming language. It is useful for splitting modules into different files and then accessing these header files into separate programs. To see header files in action, letâs look at one of our earlier examples of records.
Letâs first create a file called user.hrl and add the following code −
-record(person, {name = "", id}).
Now in our main program file, letâs add the following code −
Example
-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 from the above program, we are actually just including the user.hrl file which automatically inserts the ârecord code in it.
If you execute the above program, you will get the following output.
Output
1 âJohnâ
You can also do the same thing with macros, you can define the macro inside the header file and reference it in the main file. Letâ see an example of this −
Letâs first create a file called user.hrl and add the following code −
-define(macro1(X,Y),{X+Y}).
Now in our main program file, letâs add the following code −
Example
-module(helloworld). -export([start/0]). -include("user.hrl"). start() -> io:fwrite("~w",[?macro1(1,2)]).
If you execute the above program, you will get the following output −
Output
{3}