Reputation: 405
I have been seeing code like this usually in the start of source files in C
#ifndef _INCLUDE_GUARDS_C
#define _INCLUDE_GUARDS_C
int main()
{
}
int function1()
{
}
#endif
int function2()
{
}
I am confused about the purpose of this ..?
I am aware if the include guards define in header files, but
What is the purpose of these include guards in source files?
Why is function2()
defined outside the include guards?
Upvotes: 2
Views: 1264
Reputation: 137
Old question, but...
I think it could be used when testing the code. When testing you need access to local functions that are not defined in the header, so you include the .c
file... Yes, it's ugly. Yes, you have better options!
For the functions that are not defined in the header you don't need the include guard.
Upvotes: 0
Reputation: 137800
There is no benefit to putting include guards in a C or C++ non-header source file.
I have implemented a preprocessor from scratch and studied include guards about as much as a person can, and that is totally pointless.
As for the function outside the guards, it looks like sloppiness to me. Or, sometimes when someone has a magic incantation, they aren't sure when it is supposed to apply, so they apply it randomly.
Upvotes: 6