C++ #include and #import difference
Asked Answered
L

6

115

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

Latanya answered 5/10, 2008 at 16:50 Comment(1)
Note #import for vc++ and gcc is different from import of C++20.Zampino
R
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.

Ramiform answered 5/10, 2008 at 16:54 Comment(6)
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 supportPiracy
Curious, I wasn't aware of that. Perhaps I should have said it's a COM- and .NET-specific thing instead.Ramiform
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.Somniloquy
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).Hymettus
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).Homochromatic
Fixed link: gcc.gnu.org/onlinedocs/gcc-4.3.2/cpp/…Ikon
P
95

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.

Piracy answered 5/10, 2008 at 16:50 Comment(1)
Your MSDN links are stale. Thanks Microsoft.Photoelectric
R
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.

Ramiform answered 5/10, 2008 at 16:54 Comment(6)
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 supportPiracy
Curious, I wasn't aware of that. Perhaps I should have said it's a COM- and .NET-specific thing instead.Ramiform
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.Somniloquy
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).Hymettus
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).Homochromatic
Fixed link: gcc.gnu.org/onlinedocs/gcc-4.3.2/cpp/…Ikon
D
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.

Danieu answered 7/4, 2009 at 2:43 Comment(0)
M
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.

Mcnamee answered 22/2, 2021 at 21:24 Comment(5)
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.Alvord
import completly irrelevant? LMAO. Can you defend your answer to SO readers instead just "telling people" obvious things like what is a preprocesor directive?Mcnamee
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.Comedietta
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.Zampino
@LouisGo This is true. This question is a top 1 in Google's "C++ import" search results. Thanks!Barcellona
A
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...

Anuska answered 5/10, 2008 at 21:14 Comment(3)
Daveed was an EDG employee at the time, so I'd expect them to have so working code.Parthia
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 ;^)~Anuska
As I feared, it's a few years out: Modules in C++09?Anuska
P
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

Punctuation answered 22/1, 2010 at 0:19 Comment(1)
This doesn't answer the question.Uranous

© 2022 - 2024 — McMap. All rights reserved.