What is the difference between #import and #include in Objective-C?
The #import directive was added to Objective-C as an improved version of #include. Whether or not it's improved, however, is still a matter of debate. #import ensures that a file is only ever included once so that you never have a problem with recursive includes. However, most decent header files protect themselves against this anyway, so it's not really that much of a benefit.
Basically, it's up to you to decide which you want to use. I tend to #import headers for Objective-C things (like class definitions and such) and #include standard C stuff that I need. For example, one of my source files might look like this:
#import <Foundation/Foundation.h>
#include <asl.h>
#include <mach/mach.h>
What is the difference between #import and #include in C?
This is well-explained in the Gnu CPP (C preprocessor) manual, although the behaviour is the same in clang (and possibly other C compilers, but not MSVC):
The problem. Summary: You don't usually want to include the same header twice into a single translation unit, because that can lead to duplicate declarations, which is an error. However, since included files may themselves want to include other files, it is hard to avoid.
Some non-standard solutions (including
#import
). Summary:#import
in the including file and#pragma once
in the included file both prevent duplicate inclusion. But#pragma once
is a much better solution, because the includer shouldn't need to know whether duplicate inclusion is acceptable.
The linked document calls #import
a "deprecated extension", which is a slightly odd way of describing a feature which was never part of any standard C version. But it's not totally meaningless: many preprocessor implementations do allow #import
(which is a feature of Objective-C), so it is a common extension. Calling it deprecated is a way of saying that the extension will never be part of any C standard, regardless of how widespread implementations are.
If you want to use an extension, use #pragma once
; that also might not be present in a future standard, but changing it for a given header file will only require a change in one place instead of in every file which includes the header. C++ and even C are likely at some point to develop some kind of module feature which will allow inclusion guards to finally be replaced.
C++ #include and #import difference
#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.
Difference between `import` and `#include`? cpp20
I don't understand why
- I saw import std.core; here
You saw what you did because you read the page and that's what was written there.
- I can't import std;
This is because the C++20 standard library doesn't define modules. And because no other library can (or shouldn't) define module std
because that module name is reserved for language implementation / future standardisation.
- I can't import std.iostream;
See 2.
- I can
#include <iostream>
That header is part of the C++20 standard library.
Then why 1. works?
The MSVC documentation explains:
Although not specified by the C++20 standard, Microsoft enables its implementation of the C++ Standard Library to be imported as modules.
P.S. Support for modules is at the moment of writing only partially implemented by all major compilers with the exception of MSVC which appears to have full implementation since 19.28 (the modular standard libary is not a requirement for this).
P.P.S. Modular standard library is planned for C++23.
Difference Between includes and imports
The #import
directive is an improved version of #include
.
#import
ensures that a file is only ever included once so that you never have a problem with recursive includes.
#import ""
first check the header in project folder then goes to system library, and the #import<>
checks for system headers". In theory the locations are compiler defined and they could be implemented differently on a given platform.
What is the difference between @class and #import
I decided to refer to the documentation because I was still confused:
#import
This directive is identical to #include, except that it makes sure that the same file is never included more than once. It’s therefore preferred and is used in place of #include in code examples throughout Objective-C–based documentation.
This convention means that every interface file includes, indirectly, the interface files for all inherited classes. When a source module imports a class interface, it gets interfaces for the entire inheritance hierarchy that the class is built upon.
@class
Declarations like this simply use the class name as a type and don’t depend on any details of the class interface (its methods and instance variables), the @class directive gives the compiler sufficient forewarning of what to expect. However, where the interface to a class is actually used (instances created, messages sent), the class interface must be imported.
http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocDefiningClasses.html#//apple_ref/doc/uid/TP30001163-CH12-TPXREF123
Related Topics
Pass by Pointer & Pass by Reference
Get Size of Terminal Window (Rows/Columns)
How to Write a Std::String to a Utf-8 Text File
Avx2: Computing Dot Product of 512 Float Arrays
What Is the Safe Way to Fill Multidimensional Array Using Std::Fill
What Should a C++ Getter Return
Do Stl Iterators Guarantee Validity After Collection Was Changed
C++ Template Metaprogramming - How to Output the Generated Code
Loading a File into a Vector<Char>
How to Avoid Undefined Execution Order for the Constructors When Using Std::Make_Tuple
How to Easily Make Std::Cout Thread-Safe
How to Build Boost Version 1.58.0 Using Visual Studio 2015 (Enterprise)
C++11 Variable Number of Arguments, Same Specific Type
What's the Easiest Way to Generate Xml in C++
Access Private Member Using Template Trick
Why Can't Std::Ostream Be Moved
How to Get Current CPU and Ram Usage in C++
Should the Copy-And-Swap Idiom Become the Copy-And-Move Idiom in C++11