112

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

cjds
  • 8,268
  • 10
  • 49
  • 84
Marius
  • 57,995
  • 32
  • 132
  • 151

6 Answers6

94

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.

Community
  • 1
  • 1
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
71

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

Head Geek
  • 38,128
  • 22
  • 77
  • 87
  • 10
    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 – Brian R. Bondy Oct 05 '08 at 17:04
  • 1
    Curious, I wasn't aware of that. Perhaps I should have said it's a COM- and .NET-specific thing instead. – Head Geek Oct 05 '08 at 17:08
  • 31
    The #import supported by gcc is a nonportable way to include a header once only: . It is completely unrelated to the Microsoft COM #import. – fizzer Oct 05 '08 at 18:04
  • 5
    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). – Michael Morris Mar 29 '14 at 03:50
  • 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). – Dmytro Dec 23 '17 at 19:10
  • Fixed link: https://gcc.gnu.org/onlinedocs/gcc-4.3.2/cpp/Obsolete-once_002donly-headers.html – Andrew Aug 29 '20 at 04:53
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
  • 12,423
  • 14
  • 84
  • 114
thatha
  • 307
  • 3
  • 9
6

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.

Alex Vergara
  • 1,766
  • 1
  • 10
  • 29
  • 4
    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. – Nick Huang Feb 26 '21 at 11:42
  • 5
    import completly irrelevant? LMAO. Can you defend your answer to SO readers instead just "telling people" obvious things like **what is a preprocesor directive**? – Alex Vergara Mar 03 '21 at 08:57
  • 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. – cjds Dec 27 '21 at 04:06
  • 4
    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. – Louis Go Dec 27 '21 at 05:54
  • 1
    @LouisGo This is true. This question is a top 1 in Google's "C++ import" search results. Thanks! – RomanMitasov Feb 14 '22 at 20:03
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...

Don Wakefield
  • 8,693
  • 3
  • 36
  • 54
  • Daveed was an EDG employee at the time, so I'd expect them to have so working code. – MSalters Oct 06 '08 at 14:37
  • 1
    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 ;^)~ – Don Wakefield Oct 07 '08 at 22:54
  • 1
    As I feared, it's a few years out: [Modules in C++09?](http://groups.google.com/group/comp.lang.c++.moderated/msg/5ce3042a8de03284?dmode=source) – Don Wakefield Oct 27 '08 at 22:45
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
  • 26,356
  • 27
  • 122
  • 180
Mike Godin
  • 3,727
  • 3
  • 27
  • 29