user198729
user198729

Reputation: 63686

Why it's valid to include a header file twice in c++?

#include "DLLDefines.h"
#include "DLLDefines.h"

The above actually passed compilation, but why?

Upvotes: 8

Views: 8251

Answers (7)

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 248279

Well, it's legal because it has to be legal. Because you often include the same header multiple times without even realizing it.

You might include two headers in a .cpp file, each of which include a number of files, some of which might be included by both.

For example, all the standard library headers (say, string or vector for example) are probably included in most of your headers. So you quickly end up with the same header being indirectly included multiple times in the same .cpp file.

So in short, it has to work, or all C++ code would fall apart.

As for how it works, usually through include guards. Remember that #include just performs a simple copy/paste: it inserts the contents of the header file at the #include site.

So let's say you have a header file header.h with the following contents:

class MyClass {};

now let's create a cpp file which includes it twice:

#include "header.h"
#include "header.h"

the preprocessor expands this to:

class MyClass {};
class MyClass {};

which obviously causes an error: the same class is defined twice. So that doesn't work. Instead, let's modify the header to contain include guards:

#ifndef HEADER_H
#define HEADER_H

class MyClass {};

#endif

Now, if we include it twice, we get this:

#ifndef HEADER_H
#define HEADER_H

class MyClass {};

#endif

#ifndef HEADER_H
#define HEADER_H

class MyClass {};

#endif

And this is what happens when the preprocessor processes it:

#ifndef HEADER_H // HEADER_H is not defined, so we enter the "if" block
#define HEADER_H // HEADER_H is now defined

class MyClass {};// MyClass is now defined

#endif           // leaving the "if" block

#ifndef HEADER_H // HEADER_H *is* defined, so we do *not* enter the "if" block
//#define HEADER_H
//
//class MyClass {};
//
#endif           // end of the skipped "if" block

So, the end result is that MyClass got defined only once, even though the header was included twice. And so the resulting code is valid.

This is an important property of header files. Always define your headers so that it is valid to include them multiple times.

Upvotes: 20

bjskishore123
bjskishore123

Reputation: 6352

DLLDefines.h may also have #pragma once at the top, #pragma once ensures that file gets included only once.

Upvotes: 0

Chubsdad
Chubsdad

Reputation: 25537

As long as the multiple inclusion of header files do not violate ODR (One definition Rule) $3.2, the code is well-formed.

Upvotes: 2

JustBoo
JustBoo

Reputation: 1741

It's called an include guard.

#ifndef GRANDFATHER_H
#define GRANDFATHER_H

struct foo {
    int member;
};

#endif

Quote from Wikipedia:

In the C and C++ programming languages, an #include guard, sometimes called a macro guard, is a particular construct used to avoid the problem of double inclusion when dealing with the #include directive. The addition of #include guards to a header file is one way to make that file idempotent.

See link above for more information.

Upvotes: 1

plinth
plinth

Reputation: 49209

include has nothing to do with the C or C++ language. It is a directive to the preprocessor to bring in a file. The preprocessor doesn't care what file is brought in and it shouldn't. It might be perfectly acceptable to do this:

void Foo::SomeFunc(int cond)
{
    switch (cond) {
    case kThisCase:
#include "longFirstCase.h"
        break;
    case kSecondCase:
#include "longSecondCase.h"
        break;
    case kThirdCase:
#include "longFirstCase.h"
#include "longSecondCase.h"
        break;
    }
}

I have seen the same file included several times as a configuration mechanism as well.

Granted, there are a number of ways to factor that example that are better, but the point is that there may be perfectly good reasons why you would want to and therefore no restriction on the use.

Upvotes: 6

CB Bailey
CB Bailey

Reputation: 793249

It depends on the header file; there is no language restriction on multiple includes of the same file.

Some files are designed to be included multiple times (e.g. <assert.h> can be included multiple times to turn 'on' and 'off' assert).

Many files are safe to be included multiple times because they have include guards, others are not and should be included only once in a translation unit or even a program.

Upvotes: 8

Zaki
Zaki

Reputation: 1101

Probably you have some #define in DLLDefines.h around your code that prevents it from being included twice.

#ifndef DLLDEFINES_H
#define DLLDEFINES_H
// your code
#endif

Upvotes: 4

Related Questions