14

I am trying to check whether an #include <file.h> has already been declared using C++ macro.

Example

In file.h I do:

#ifndef FILE.H
#define FILE.H
class A
{
  //do routines 
};
#endif

In a second file called second.h I want to check if file.h has been included already.

Typical psuedo-code:

#ifndef "file.h"
#include "file.h"
#endif

#ifndef SECOND.H
#define SECOND.H
class second
{
  //do routine
};
#endif

I have tried a few #ifndef directives but no joy. Do anyone know how to achieve this?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Bitmap
  • 12,402
  • 16
  • 64
  • 91
  • 1
    Firstly, #ifndef FILE.H is not legal, perhaps you meant #ifndef FILE_H. Secondly you don't need to do what you are trying to do, #ifndef FILE_H in file.h is all you need, file second.h just needs to say #include "file.h", nothing more. – john Aug 07 '11 at 15:52

6 Answers6

14

Everybody else has covered correct inclusion of include guard macros.

File.h

#ifndef   FILE_H
#define   FILE_H
// Code here
#endif

But everybody else missed the second part of your question on correct usage:

This is an example of bad usage:

#ifndef  FILE_H  
#include "file.h"
#endif

#ifndef SECOND_H
#define SECOND_H
class second
{
  //do routine
};
#endif

This is not correct (though correct may be too strong).

The include guards in each file should surround their entire content. So, in the second file, the #include "file.h" should be inside the include guards.

Also you do not need to test for inclusion that will be done inside the file itself. Thus the file should look like this:

#ifndef SECOND_H
#define SECOND_H

#include "file.h"

class second
{
  //do routine
};
#endif

Also your macro guards are way too small. There is a high likelihood that these will be used by somebody else; this will cause all sorts of weird collisions.

A good way to make the guards unique is to use a prefix, your full name (or nick name), or if you have your own registered website then uses its domain name. I then also add part of the directory hierarchy that includes the file.

#ifndef    WEBSITE1_NAMESPACE1_NAMESPACE2_FILENAME_H
#define    WEBSITE1_NAMESPACE1_NAMESPACE2_FILENAME_H

#endif

Another alternative (especially if you are working on Windows) is to generate a GUID. Just generate a new one for each file.

Final point: only include other files from header files if their content is absolutely necessary. You only need to include "file.h" if this file has a type definition that class second depends on. If your class only uses that class as a pointer or a reference then prefer to use forward declaration rather including the header file.

The scenarios in which you must #include are:

  • second is a child of first.
  • second has member(s) that are first objects
  • second has method(s) that take first objects as parameters
  • Second has method(s) that return first objects as a result

Notice that I use the term "objects". If they are references or pointers then this does not count. In these cases you can use forward declaration to solve the problem.

Community
  • 1
  • 1
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • `SECOND.H` is not a legal macro name, and I don't see the issue with that "bad usage example" (though the check for `FILE_H` is redundant). Although conventionally you would protect the _entire_ header contents with a guard -- including other `#include` statements -- there is no reason that this "must" be done. – Lightness Races in Orbit Aug 07 '11 at 17:50
  • @Tomalak Geret'ka: Thanks for all the fixes. – Martin York Aug 07 '11 at 18:14
8

in file.h:

#ifndef FILE_H
#define FILE_H
...
...
...
#endif

Then check whether file.h is included with:

#ifdef FILE_H
nobody
  • 10,599
  • 4
  • 26
  • 43
2

In file.h

#ifndef FILE_H_
#define FILE_H_
#else
#error file.h has been included previously
#endif

But this will cause you program to not compile if you include file.h more than once. What you probably want is

#ifndef FILE_H_
#define FILE_H_

// contents of file.h go here

#endif

This will prevent compilation errors / warnings due to multiple inclusion.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • 2
    The naming pattern you use for your inclusion guards infringes on the set of names reserved for the implementation. http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – Benjamin Lindley Aug 07 '11 at 15:55
2

The simple reason for the #ifndef FILE_H line in the header is to make it such that, on second and further inclusions, the file is a no-op. Those # lines taken together are known as the "include guard".

Thus, simply doing #include "file.h" will already do what you're trying to do in the second file. (The preprocessor will probably open and read the file before discarding its contents, but it's reasonable to assume it's in cache, and thus there is no compiler performance gain to be expected in the general case.

If you'd complain a file is not included, you could just include it yourself. Perhaps you want to prohibit a file from being included when another file is included, so they are either-or; or strict ordering of includes (probably a symptom of a design problem). Or use alternate code for when something has (not) been included.

In such cases, #ifdef X or #ifndef X where X matches what is used in the include guard, will work.

MaHuJa
  • 3,148
  • 1
  • 18
  • 6
1

The typical way of doing something like this is:

#ifndef _FILE_H_
#define _FILE_H_

// some code

#endif

This will prevent the same header from being included multiple times.

Robert S. Barnes
  • 39,711
  • 30
  • 131
  • 179
0

All the answers are covered. Just a tip for larger projects with multiple includes, it is important to use #define FILE_H before you include. Otherwise you'll get the error recursively. e.g

MAIN
#ifndef FILE1_H
  #include "file1.h"
  #define FILE1_H
#endif

FILE1
#ifndef FILE2_H
  #include "file2.h"
  #define FILE2_H
#endif

FILE2
#ifndef FILE1_H
 #include "file1.h" **<-- FILE2_H and FILE1_H aren't yet defined so it'll be resursively included**
 #define FILE1_H
#endif