122

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

cjds
8,47611 gold badges56 silver badges86 bronze badges
asked Oct 5, 2008 at 16:50
1
  • 9
    Note #import for vc++ and gcc is different from import of C++20. Commented Dec 27, 2021 at 5:55

7 Answers 7

96

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.

answered Oct 5, 2008 at 16:50
Sign up to request clarification or add additional context in comments.

Comments

72

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

answered Oct 5, 2008 at 16:54

6 Comments

This is not true. 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
Curious, I wasn't aware of that. Perhaps I should have said it's a COM- and .NET-specific thing instead.
The #import supported by gcc is a nonportable way to include a header once only: <a href="gcc.gnu.org/onlinedocs/gcc-4.3.2/cpp/…>. It is completely unrelated to the Microsoft COM #import.
The GCC #import is actually an Objective-C preprocessor command that happens to work with .c files in gcc and clang(except in pedantic mode).
import is actually pretty clever; dlls can export the headers as a text string that can be loaded on any platform(granted the dll needs to be recompiled) before compiling the c code, and paste the string into the source code before compilation; effectively eliminating the need for header files altogether. I think this was part of the technology that allowed java and other dynamic languages not to have header files, but still have type awareness(Granted they tend to compress the type info rather than keep it as string).
|
11

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

sjngm
13k16 gold badges90 silver badges118 bronze badges
answered Apr 7, 2009 at 2:43

Comments

11

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.

answered Feb 22, 2021 at 21:24

5 Comments

c++20 import is completely irrelevant because beginning with preprocessor character '#' indicating it is a preprocessor directive, whiles module "import" is a keyword starting with a module delcaration.
import completly irrelevant? LMAO. Can you defend your answer to SO readers instead just "telling people" obvious things like what is a preprocesor directive?
I think the distinction @NickHuang is trying to draw is between #import and import. This question is re: the MS #import statement and specifically relates to visual-c++ as in the tags. The import keyword is a newish concept and didn't exist (in the standard) when this question was asked.
I believe whoever search for #include and import would find this question in the first place as well. So it's relevant because of the limitation of search engine.
@LouisGo This is true. This question is a top 1 in Google's "C++ import" search results. Thanks!
4

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

answered Oct 5, 2008 at 21:14

3 Comments

Daveed was an EDG employee at the time, so I'd expect them to have so working code.
I sure hope they've done the requisite legwork, because it would be very nice to move from '#include' to an import mechanism. But I've heard nary a peep on this feature, and I'm pretty sure it's not in C++0X. Maybe sometime before I retire ;^)~
As I feared, it's a few years out: Modules in C++09?
4

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

#import is a deprecated GCC extension

bluish
27.5k28 gold badges126 silver badges185 bronze badges
answered Jan 22, 2010 at 0:19

1 Comment

This doesn't answer the question.
0

C++ imports are a very new language feature (modules), supported only by c++ compilers.

Ideally speaking, the only difference between includes and imports should be the compile times. The latter being way faster.

Also note, if we are talking about module, then its import without hash prefixed

answered Oct 4, 2024 at 5:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.