C++ #include and #import difference

C++Visual C++GccImportInclude

C++ Problem Overview


What is the difference between #include and #import in C++?

C++ Solutions


Solution 1 - C++

Import in VC++: #import is for type libraries or .tlbs (COM stuff).

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.

The #import directive was introduced by Microsoft as an extension to the C++ language. You can read about it at this MSDN article.

The #import directive is also used with .NET / CLI stuff.

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)

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

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.

Solution 2 - C++

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

#include is a standard C/C++ preprocessor statement, used for including header (or occasionally other source code) files in your source code file.

Solution 3 - C++

#import is overall a solution to the usual

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

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

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

Solution 4 - C++

import was 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...

Solution 5 - C++

Please note that in gcc 4.1, #import is deprecated. If you use it, you will get warning:

>#import is a deprecated GCC extension

Solution 6 - C++

Should this post be updated?

Now, since the C++20 standard is outta there, we can get into scope "modules" with the import statement.

https://en.cppreference.com/w/cpp/language/modules

In terms of compiling speed when multiple modules are called from different parts of the code, import statement seems to be quicker than the old #include preprocesor directive.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMariusView Question on Stackoverflow
Solution 1 - C++Brian R. BondyView Answer on Stackoverflow
Solution 2 - C++Head GeekView Answer on Stackoverflow
Solution 3 - C++thathaView Answer on Stackoverflow
Solution 4 - C++Don WakefieldView Answer on Stackoverflow
Solution 5 - C++Mike GodinView Answer on Stackoverflow
Solution 6 - C++PyzyryabView Answer on Stackoverflow