C++ 包含和导入的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/172262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ include and import difference
提问by Marius
What is the difference between #include
and #import
in C++?
在 C++ 中#include
和#import
在 C++ 中有什么区别?
采纳答案by Head Geek
#import
is a Microsoft-specific thing, apparently for COM or .NET stuff only.
#import
是特定于 Microsoft 的东西,显然仅适用于 COM 或 .NET 的东西。
#include
is a standard C/C++ preprocessor statement, used for including header (or occasionally other source code) files in your source code file.
#include
是标准的 C/C++ 预处理器语句,用于在您的源代码文件中包含头文件(或偶尔的其他源代码)文件。
回答by Brian R. Bondy
Import in VC++:#import
is for type libraries or .tlbs (COM stuff).
在 VC++ 中导入:#import
用于类型库或 .tlbs(COM 内容)。
The content of the type library is converted into C++ classes, mostly describing the COM interfaces for you automatically, and then it is included into your file.
类型库的内容被转换为 C++ 类,主要是自动为您描述 COM 接口,然后将其包含到您的文件中。
The #import
directive was introduced by Microsoft as an extension to the C++ language. You can read about it at this MSDN article.
该#import
指令由 Microsoft 作为 C++ 语言的扩展引入。您可以在这篇 MSDN 文章中阅读它。
The #import
directive is also used with .NET / CLI stuff.
该#import
指令还用于 .NET/CLI 的东西。
Import in gcc:The import in gcc is different from the import in VC++. It is a simple way to include a header at most once only. (In VC++ and GCC you can do this via #pragma
once as well)
在 gcc 中导入:gcc 中的导入与 VC++ 中的导入不同。这是一种最多只包含一次标题的简单方法。(在 VC++ 和 GCC 中,您也可以通过#pragma
一次执行此操作)
The #import
directive was officially undeprecated by the gcc team in version 3.4 and works fine 99% of the time in all previous versions of gcc which support
该#import
指令在 3.4 版中被 gcc 团队正式弃用,并且在所有支持
Include:#include
is for mostly header files, but to prepend the content to your current file. #include
is part of the C++ standard. You can read about it at this MSDN article.
Include:#include
主要用于头文件,但将内容添加到当前文件中。#include
是 C++ 标准的一部分。您可以在这篇 MSDN 文章中阅读它。
回答by thatha
#import
is overall a solutionto the usual
#import
总体上是对通常的解决方案
#ifndef ...
#define ...
#include ...
#endif
work-around. #import
includes a file only if it hasn't been included before.
解决办法。#import
仅当以前未包含文件时才包含该文件。
It might be worth noting that Apple's Objective-C also uses #import
statements.
值得注意的是,Apple 的 Objective-C 也使用了#import
语句。
回答by Don Wakefield
importwas also one of the keywords associated with n2073, Modules in C++, proposed to the language committee by Daveed Vandevoorde in September 2006. I'm not enough of a language geek to know if that proposal was definitively shelved or if it's awaiting an implementation (proof of concept) from the author or someone else...
import也是与n2073相关的关键字之一,C++ 中的模块,由 Daveed Vandevoorde 于 2006 年 9 月向语言委员会提出。我不是一个语言极客,无法知道该提案是否被最终搁置或是否正在等待实施(概念证明)来自作者或其他人......
回答by Mike Godin
Please note that in gcc 4.1, #import
isdeprecated. If you use it, you will get warning:
请注意,在 gcc 4.1 中,#import
已弃用。如果你使用它,你会得到警告:
#import
is a deprecated GCC extension
#import
不推荐使用的 GCC 扩展