开发者

What MSVC++ 2008 Express Editon compiler does and doesn't

开发者 https://www.devze.com 2023-03-13 08:08 出处:网络
I\'ve been wondering if the msvc++ 2008 compiler takes care of multiple header includes of the same file,开发者_StackOverflow considering this example:

I've been wondering if the msvc++ 2008 compiler takes care of multiple header includes of the same file,开发者_StackOverflow 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?


#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.


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.


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.


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.


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.

0

精彩评论

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