2

I've been wondering if the msvc++ 2008 compiler takes care of multiple header includes of the same file, considering this example:
main.cpp

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

Will the compiler include this file multiple times or just one? (I'm aware I can use the #ifndef "trick" to prevent this from happening) Also, if I include "header.h" which contains 10 functions, but I only call or use 2, will it still include all 10 or just the 2 I need and all of their needs?

Benjamin
  • 531
  • 1
  • 7
  • 17

5 Answers5

4

#include is basically a synonym for "copy-and-paste". If you do identical #includes, the contents of that header file will be copy-and-pasted twice, sequentially.

As to your second question, it doesn't really make sense. #includes are executed by the preprocessor, which runs before the compiler and the linker. The preprocessor doesn't know or care what the content of the header file is, it simply copy-and-pastes it in. The linker may be able to eliminate unnecessary functions, but that's completely independent of the preprocessor.

Oliver Charlesworth
  • 252,669
  • 29
  • 530
  • 650
  • How can you be sure MSVC doesn't take care of this as some sort of preprocessing optimization? :) – Armen Tsirunyan Jun 15 '11 at 13:40
  • @Armen: Well, I can't be sure! But I can't imagine that it would do such a thing, because there are occasions where you really do want to `#include` something multiple times (e.g. X macros). – Oliver Charlesworth Jun 15 '11 at 13:41
3

No, the compiler (or, more accurately, the pre-processor) doesn't take care of this "automatically". Not in Visual C++ 2008, or in any other version. And you really wouldn't want it to.

There are two standard ways of going about this. You should choose one of them.

The first is known as include guards. That's the "#ifndef trick" you mentioned in your question. But it's certainly not a "trick". It's the standard idiom for handling this situation when writing C++ code, and any other programmer who looks at your source file will almost certainly expect to see include guards in there somewhere.

The other takes advantage of a VC++ feature (one that's also found its way into several other C++ toolkits) to do essentially the same thing in a way that's somewhat easier to type. By including the line #pragma once at the top of your header file, you instruct the pre-processor to only include the header file once per translation unit. This has some other advantages over include guards, but they're not particularly relevant here.

As for your second question, the linker will take care of "optimizing" out functions that you never call in your code. But this is the last phase of compilation, and has nothing to do with #include, which is handled by the pre-processor, as I mentioned above.

Community
  • 1
  • 1
Cody Gray
  • 222,280
  • 47
  • 466
  • 543
  • Alright, thanks :). So just to set things straight: to prevent multiple inclusions of the same header file I have to implement a so called 'include guard' (thanks for adding a new word to my "programmers dictionary" ;)) and the linker WILL remove "unnecessary" code from the final executable? – Benjamin Jun 15 '11 at 14:29
  • @Benjamin: Those two things are unrelated. Compilation in C++ occurs in several different stages. The `#include` directives are resolved by the pre-processor in the *first* stage of compilation. As Oli's answer suggests, it essentially copies-and-pastes the contents of the headers into your source files. That's why you need the include guards (or `#pragma once`) to prevent it from copying and pasting it more than once. At a *later* phase of compilation, called the **link stage**, the linker is going to see what functions you call and only include those that you actually use. – Cody Gray Jun 15 '11 at 14:31
  • @Benjamin: Read the answers to [this question](http://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work) to get a better understanding of how all the steps actually work. – Cody Gray Jun 15 '11 at 14:33
1

The MSVC 20xx preprocessor (not the compiler -- the compiler never sees preprocessor directives) does not in any sense "take care of" multiple #includes of the same file. If a file is #included twice, the preprocessor obeys the #includes and includes the file two times. (Just imagine the chaos if the preprocessor even thought about trying to correct your source file's "bad" #include behavior.)

Because the preprocessor is so meticulous and careful about following your instructions, each #included file must protect itself from being #included twice. That protection is what we see when we find lines like these at the top of a header file:

 #ifndef I_WAS_ALREADY_INCLUDED   // if not defined, continue with include
 #define I_WAS_ALREADY_INCLUDED   // but make sure I'm not included again

    [ header-file real contents ]

 #endif  // I_WAS_ALREADY_INCLUDED

When you write a header file, you must always be sure to protect it in this way.

Pete Wilson
  • 8,198
  • 6
  • 33
  • 50
0

Why do you care? It doesn't add really much burden on the compiler because the compiler conditionally (with #ifdefs, for example) excludes code it doesn't need to compile.

Armen Tsirunyan
  • 120,726
  • 52
  • 304
  • 418
0

Preprocessor will include 2 times these headers. Thats why guards in header files are required. As far as I know the linker is most cases will remove code (functions) that are newer used to reduce executable file size.

Zuljin
  • 2,517
  • 13
  • 13