开发者

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

开发者 https://www.devze.com 2023-01-12 07:27 出处:网络
#include \"DLLDefines.h\" #include \"DLLDefines开发者_如何转开发.h\" The above actually passed compilation, but why?Well, it\'s legal because it has to be legal. Because you often include the same h
#include "DLLDefines.h"
#include "DLLDefines开发者_如何转开发.h"

The above actually passed compilation, but why?


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.


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.


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.


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


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消