Skip to main content
gamedev.net
🔒 Locked

Header files

Started by pdstatha 3 replies 450+ views
Original Post
pdstatha
pdstatha
Ok this has probably been explained over and over in these forums. But can some explain all that #ifndef and endif stuff that I''ve seen in header files. What does it mean, is it something to do with not having to include a header file more than once. Snippet of code + plus explanation needed me thinks.
Fruny
Fruny
These are preprocessor macro. #ifndef means 'if not defined'. If the symbol being tested has been #defined, the preprocessor throws the whole block away. If it hasn't been, usually the appropriate #define follows, so that the block gets included only once, even if the header itself got #included several times.


Assume foo.h:

#ifndef FOO_H
#define FOO_H
int foo();
#endif

Now write

#include "foo.h"
#include "foo.h"

Expand:

#ifndef FOO_H
#define FOO_H
int foo();
#endif
#ifndef FOO_H
#define FOO_H
int foo();
#endif

At the level of the first #ifndef, FOO_H is undefined, therefore, the #if/#endif block is preserved, which #defines FOO_H
At the level of the second #ifndef, FOO_H is now defined. The preprocessor throws the block away.

// #ifdef FOO_H
#define FOO_H
int foo();
// #endif

// #ifndef FOO_H
// #define FOO_H
// int foo();
// #endif


Or read Kylotan's article on source code organisation.
Edit: The article is there.


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 16, 2002 7:30:39 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
pdstatha
pdstatha
So correct me if i'm wrong. If in my header file I had the following.

  
#ifndef MYHEADER_H
#define MYHEADER_H

#include <string>

#endif



And then in my C++ file I had

      
#include "myheader.h"


Then I wouldn't need to include the string header file again????


[edited by - pdstatha on June 16, 2002 11:18:16 AM]
erjo
erjo
well, not quite.
The check stops globals from being defined twice, which causes an error. you still have to include the file in every sourcefile that needs it, but you wont get errors on all the globals.

Eric
Fruny
Fruny
Using include guards (as they are called), only ensure that the headers will not redundantly be included into your code, which may cause it to fail to compile (e.g. compilers reject duplicate definitions of variables). They also serve to break recursive inclusions (header A includes header B and conversely).

Aside from that, your assertion is right. Including <string> within "myheader.h" will propagate <string> to any file that includes "myheader.h". Note that since <string> is itself protected by include guards, if a file includes both "myheader.h" and <string>, one of the <string> will be silently removed by the preprocessor. (The same would happen if you included "myheader.h" twice.).

However, you have to remember that each source (.cpp) file is compiled independently from the others. Which means that you must include the whole set of headers that source file requires. The fact that yo included "myheader.h" in foo.cpp doesn''t affect bar.cpp (which may, or may not require its own copy of the header) at all

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.