Reputation: 11
I have three files and want to compile it.
The first file is app.c
#include"sum.h"
int main (void)
{
sum();
return 0;
}
The second files is sum.h
#ifndef SUM_H
#define SUM_H
void sum ();
#endif
and the third file sum.c
#include"sum.h"
void sum ()
{
return;
}
and I write these statements:
#ifndef SUM_H
#define SUM_H
...
...
...
#endif
to prevent the multiple definitions of the content of sum.h
.
During compiling the app.c
will enter the preprocessing stage and then sum.c
enter the preprocessing stage my question when the sum.c
enters how the preprocessor know that SUM_H
is defined in the last file of myapp?
I think that each source file enter the preprocessing stage and compiling stage individually.
Upvotes: 1
Views: 281
Reputation: 206518
Yes, each file is compiled separately and hence include guards only prevent multiple inclusion of a header file in the same translation unit and not across different translation units.
When app.c
is pre-processed by the pre-compiler only checks if SUM_H
is already defined within app.c
only. This avoids sum.h
from being included multiple times through different headers in this present translation unit only. This is because as soon as sum.h
gets included once SUM_H
will be defined.
Upvotes: 2