Skip to main content

#ifndef statement

Submitted by Kane on
Forum

Hi all,

In a book I have just finished reading I came across the following code and was wondering what it does:

#ifndef MONSTERNPC_H
#define MONSTERNPC_H

#endif

I am not quite sure what the #ifndef statement does, so can someone explain it for me?[?]

Submitted by lava monkey on Thu, 23/10/03 - 2:40 AM Permalink

#ifndef = if not defined
putting that around classes is to stop it from being included more than once
i prefer to use: #pragma once
it does the same thing as all that, but for the whole file.

Submitted by tachyon on Thu, 23/10/03 - 3:41 AM Permalink

for portability, I would use the #ifndef trick rather than #pragma once
not all preprocessors support #pragma once
(also gcc says its deprecated)

Submitted by Blitz on Thu, 23/10/03 - 6:12 AM Permalink

Using #pragma's is generally a bad idea if you want to write code that is compatible across multiple compilers, as #pragma's are compiler dependent. However, if you know that the code will only be compiled on a single compiler, then it's fine to use them.
The worst case scenario for using #pragma's is the same command does one thing on one compiler, and something different on another compiler which screws everything up :P
I guess that might be a reason to always RTFM first :)
However, to get back on topic and give a slightly more verbose explanation of what lava_monkey said...
When compiling, the compiler has a preprocessor which goes through files before actually compiling them. The preprocessor keep track of a bunch of pseudo variables that are defined by #define statements. A #ifndef statement checks if a variable exists, and only executes if the variable (token) does not exist.
So,
#ifndef TOKEN
#define TOKEN

// Put definitions here

#endif // TOKEN

is kind of similar to
if ( !TOKEN )
{
TOKEN = true;

// put definitions here
} // if

However, the #ifndef etc. is checked at compile time, not run time. I hope that analogy makes sense.
The basic idea behind this, is that defining a class multiple times with the same name will either cause compile errors or unexpected results. So, if you included the same header file into two source files without the "guards" the class would be defined twice and cause problems. By putting the "guards" in the header file, you make sure that that header file will only be parse once by the compiler, because the second time the compiler tries to parse that file, it will go "oh look, MONSTERNPC_H already exists, so i won't bother looking at anything til i get to the next #endif".
I wonder if that all amde sense :)
Check out the preprocessor in your favourite c/c++ book for more info :)
CYer, Blitz

Submitted by Kane on Thu, 23/10/03 - 6:56 PM Permalink

yes that made almost perfect sense...[;)]

thanks for all that yall...

Posted by Kane on
Forum

Hi all,

In a book I have just finished reading I came across the following code and was wondering what it does:

#ifndef MONSTERNPC_H
#define MONSTERNPC_H

#endif

I am not quite sure what the #ifndef statement does, so can someone explain it for me?[?]


Submitted by lava monkey on Thu, 23/10/03 - 2:40 AM Permalink

#ifndef = if not defined
putting that around classes is to stop it from being included more than once
i prefer to use: #pragma once
it does the same thing as all that, but for the whole file.

Submitted by tachyon on Thu, 23/10/03 - 3:41 AM Permalink

for portability, I would use the #ifndef trick rather than #pragma once
not all preprocessors support #pragma once
(also gcc says its deprecated)

Submitted by Blitz on Thu, 23/10/03 - 6:12 AM Permalink

Using #pragma's is generally a bad idea if you want to write code that is compatible across multiple compilers, as #pragma's are compiler dependent. However, if you know that the code will only be compiled on a single compiler, then it's fine to use them.
The worst case scenario for using #pragma's is the same command does one thing on one compiler, and something different on another compiler which screws everything up :P
I guess that might be a reason to always RTFM first :)
However, to get back on topic and give a slightly more verbose explanation of what lava_monkey said...
When compiling, the compiler has a preprocessor which goes through files before actually compiling them. The preprocessor keep track of a bunch of pseudo variables that are defined by #define statements. A #ifndef statement checks if a variable exists, and only executes if the variable (token) does not exist.
So,
#ifndef TOKEN
#define TOKEN

// Put definitions here

#endif // TOKEN

is kind of similar to
if ( !TOKEN )
{
TOKEN = true;

// put definitions here
} // if

However, the #ifndef etc. is checked at compile time, not run time. I hope that analogy makes sense.
The basic idea behind this, is that defining a class multiple times with the same name will either cause compile errors or unexpected results. So, if you included the same header file into two source files without the "guards" the class would be defined twice and cause problems. By putting the "guards" in the header file, you make sure that that header file will only be parse once by the compiler, because the second time the compiler tries to parse that file, it will go "oh look, MONSTERNPC_H already exists, so i won't bother looking at anything til i get to the next #endif".
I wonder if that all amde sense :)
Check out the preprocessor in your favourite c/c++ book for more info :)
CYer, Blitz

Submitted by Kane on Thu, 23/10/03 - 6:56 PM Permalink

yes that made almost perfect sense...[;)]

thanks for all that yall...