Given the following headers:
(1) stackinfo.h
/*
* Este tipo define la información que contiene la pila (el contenido)
*/
typedef struct pilainfo
{
int id;
char matricula[10];
char dni[10];
char nombre[50];
char mail[50];
float nota;
} tdata;
(2) stackvector.h
#define MAXPILA 50
#include "pilainfo.h"
/*
* El tipo pilavector define la pila y el indice a la cima
* Se accederá mediante los habituales servicios push/pop
*/
typedef struct pilavector {
tdata datos[MAXPILA];
int top;
} tpv;
Given test.c :
#include <stdio.h>
#include "pilainfo.h"
#include "pilavector.h"
tpv pv;
If you try to compile, you'll see quite a few errors thrown. They are divided into two types of errors : the first ones to jump are about redefinition of variables and the second ones about type conflicts . I am sure that the latter are a direct consequence of the former .
The first ones seem to arise due to some kind of conflict with the headers , but I don't understand what is wrong. Headers are correctly defined and correctly included in test.c.
This problem has arisen in a larger program where stackvector.h also has method headers that then have to be developed within test.c. In turn, test.c will serve as an auxiliary program to a larger one that is the one that directs the entire execution, that is, the one with main.
The error occurs because you are not using guards in your headers.
A typical C header should have the following form:
In some cases you can also find it like this:
The basic difference between both examples is that the first one is a standard solution that will always work while the second one might not work on some compilers.
The directives in surround the header code (in the first case) prevent the header itself from being included multiple times in the code. This is done this way because numerous compilation errors are avoided:
But why do these errors occur?
To understand it, you must first understand how the compilation process works in C.
When we try to compile a file in C, the first thing that goes into action is the preprocessor. This program walks through the file and replaces all preprocessor directives with something else:
Conditional directives
#ifdef
will#endif
delete the code they contain if the condition is not met .Macros
#define
are replaced by the corresponding codeIncludes are replaced by the file they link to
This process is repeated until there are no preprocessor directives left in the code.
That is, in your case, the following will happen in the file
prueba.c
:original code:
The first include is replaced:
The second include is replaced:
The third include is replaced:
Another include has appeared ... is replaced:
As you can see, now we have a problem and that is that as much
pilainfo
astdata
appears defined twice:This problem is solved by defining the guards in the headers:
stackinfo.h
stackvector.h
Now the code generated by the preprocessor will vary slightly:
original code:
The first include is replaced:
The second include is replaced:
The conditional is processed. Since the symbol
PILAINFO_H
is not defined, the code remains:The third include is replaced:
The conditional is processed.
PILAVECTOR_H
is not defined, so the code surrounding the conditional remains intact:One is left
#include
, it is replaced:The conditional is processed. Now we see that the symbol
PILAINFO_H
is already defined above, then the conditional will simply disappear:As you can see, this means that the symbol that was repeated is no longer repeated, avoiding the compilation errors that were being generated.