954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Understanding header files in C++

Hello everybody,

While reading C++, I have been looking into iostream header file to understand a bit more about header file organization. I am not able to understand much of the code in there.
For example the header starts with #ifndef _GLIBCXX_IOSTREAM. Does this check whether _GLIBCXX_IOSTREAM has been defined or not? If so then who is supposed to use this variable _GLIBCXX_IOSTREAM? I know it is not a good question but I am kind of curious.

psandip
Newbie Poster
3 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Any line that starts with '#' is a "preprocessor directive" . This particular one sounds like what is called a "header guard" or an "inclusion guard". It should be immediately followed by something along the lines of

#define _GLIBCXX_IOSTREAM 1


. At the end of the file, there should also be a statement similar to this:

#endif /* _GLIBCXX_IOSTREAM */


This statement ends the statement block created by the #ifndef.

The #ifndef statement checks to see "if _GLIBCXX_IOSTREAM is NOT defined". If it finds that it is NOT defined, it returns true and executes the statements contained within its statement block. The next statement, #defines _GLIBCXX_IOSTREAM. As a result, _GLIBCXX_IOSTREAM now is defined. This causes #ifndef _GLIBCXX_IOSTREAM to come false if is #included again so that it doesn't double up the #include. Here is more C++ - Specific preprocessor information.

This combination of statements is used to prevent the header being included in the code more than once which can cause compilation and linking issues.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

Hello Fbody,

Thanks for the reply. Now I think I can understand what this means. _GLIBCXX_IOSTREAM is just a flag name for use in the header guard and the name has no other significance. PLease correct me if I am wrong.

psandip
Newbie Poster
3 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

That's essentially correct. It's actually what's called a "macro". It checks to see if the macro already exists. If it doesn't, the #define creates the macro.

These types of macros simply exist, and make compiling easier. They really are of no other use within your program.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

Many thanks :)

psandip
Newbie Poster
3 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

what is 1(one) at the end of #define _GLIBCXX_IOSTREAM 1.????

what is the 1(one) use of this????

jaswanth424
Newbie Poster
1 post since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

what is 1(one) at the end of #define _GLIBCXX_IOSTREAM 1.????

what is the 1(one) use of this????


Did you even read the thread? It's all laid out for you right in the thread. The "constant" _GLIBCXX_IOSTREAM has no use in the program other than to act as a header guard. You would not use it any where else.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: