views:

6603

answers:

5

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

+17  A: 

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.

Brian R. Bondy
+15  A: 

#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
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
... which support #import
Brian R. Bondy
Curious, I wasn't aware of that. Perhaps I should have said it's a COM- and .NET-specific thing instead.
Head Geek
The #import supported by gcc is a nonportable way to include a header once only: <a href="http://gcc.gnu.org/onlinedocs/gcc-4.3.2/cpp/Obsolete-once_002donly-headers.html"/>. It is completely unrelated to the Microsoft COM #import.
fizzer
+1  A: 

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
Daveed was an EDG employee at the time, so I'd expect them to have so working code.
MSalters
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
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
+2  A: 

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

Ian
+2  A: 

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

Mike Godin