什么是 MSVC++ 2008 Express Editon 编译器有和没有

发布于 2024-11-15 16:40:11 字数 293 浏览 1 评论 0原文

我一直想知道 msvc++ 2008 编译器是否处理同一文件的多个标头包含,考虑以下示例:
ma​​in.cpp

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

编译器会多次包含该文件还是仅包含一次? (我知道我可以使用#ifndef“技巧”来防止这种情况发生) 另外,如果我包含包含 10 个函数的“header.h”,但我只调用或使用 2 个函数,它是否仍包含所有 10 个函数或仅包含我需要的 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

々眼睛长脚气 2024-11-22 16:40:11

#include 基本上是“复制粘贴”的同义词。如果您执行相同的#include,则该头文件的内容将按顺序复制并粘贴两次。

至于你的第二个问题,其实没有什么意义。 #include预处理器执行,该预处理器在编译器链接器之前运行。预处理器不知道也不关心头文件的内容是什么,它只是将其复制并粘贴进去。链接器也许能够消除不必要的函数,但这完全独立于预处理器。

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

不一样的天空 2024-11-22 16:40:11

不,编译器(或更准确地说,预处理器)不会“自动”处理此问题。 Visual C++ 2008 或任何其他版本中没有。你真的不希望这样。

有两种标准方法可以解决此问题。您应该选择其中之一。

第一个被称为包含防护。这就是您在问题中提到的“#ifndef 技巧”。但这肯定不是“诡计”。这是编写 C++ 代码时处理这种情况的标准习惯用法,任何其他查看源文件的程序员几乎肯定会期望在其中的某处看到包含防护。

另一个利用 VC++ 功能(该功能也出现在其他几个 C++ 工具包中)以更容易键入的方式完成基本相同的操作。通过在头文件顶部包含行 #pragma Once,您可以指示预处理器每个翻译单元仅包含头文件一次。与包含防护相比,还有一些其他优点,但它们在这里并不是特别相关。

至于你的第二个问题,链接器将负责“优化”你从未在代码中调用的函数。但这是编译的最后阶段,与#include 无关,它由预处理器处理,正如我上面提到的。

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.

快乐很简单 2024-11-22 16:40:11

MSVC 20xx 预处理器(不是编译器——编译器永远看不到预处理器指令)在任何意义上都不会“处理”同一文件的多个#include。如果某个文件被 #included 两次,则预处理器将遵循 #includes 并包含该文件两次。 (想象一下,如果预处理器甚至考虑尝试纠正源文件的“不良”#include 行为,会出现什么样的混乱。)

由于预处理器非常仔细且谨慎地遵循您的指令,因此每个 #included 文件必须保护自己不被 #included 两次。当我们在头文件顶部找到类似这样的行时,我们就会看到这种保护:

 #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

当您编写头文件时,您必须始终确保以这种方式保护它。

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.

关于从前 2024-11-22 16:40:11

你为什么关心?它并没有给编译器增加太多负担,因为编译器有条件地(例如使用#ifdef)排除不需要编译的代码。

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.

も星光 2024-11-22 16:40:11

预处理器将包含 2 倍的这些标头。这就是为什么需要头文件中的保护。
据我所知,链接器在大多数情况下会删除较新用于减少可执行文件大小的代码(函数)。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文