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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:26:02  来源:igfitidea点击:

C++ include and import difference

c++visual-c++gccimportinclude

提问by Marius

What is the difference between #includeand #importin C++?

在 C++ 中#include#import在 C++ 中有什么区别?

采纳答案by Head Geek

#importis a Microsoft-specific thing, apparently for COM or .NET stuff only.

#import是特定于 Microsoft 的东西,显然仅适用于 COM 或 .NET 的东西。

#includeis 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++:#importis 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 #importdirective 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 #importdirective 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 #pragmaonce as well)

在 gcc 中导入:gcc 中的导入与 VC++ 中的导入不同。这是一种最多只包含一次标题的简单方法。(在 VC++ 和 GCC 中,您也可以通过#pragma一次执行此操作)

The #importdirective 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:#includeis for mostly header files, but to prepend the content to your current file. #includeis part of the C++ standard. You can read about it at this MSDN article.

Include:#include主要用于头文件,但将内容添加到当前文件中。#include是 C++ 标准的一部分。您可以在这篇 MSDN 文章中阅读它。

回答by thatha

#importis overall a solutionto the usual

#import总体上是对通常的解决方案

#ifndef ...
#define ...
#include ...
#endif

work-around. #importincludes a file only if it hasn't been included before.

解决办法。#import仅当以前未包含文件时才包含该文件。

It might be worth noting that Apple's Objective-C also uses #importstatements.

值得注意的是,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, #importisdeprecated. If you use it, you will get warning:

请注意,在 gcc 4.1 中,#import弃用。如果你使用它,你会得到警告:

#importis a deprecated GCC extension

#import不推荐使用的 GCC 扩展