Difference Between includes and imports [duplicate]
Asked Answered
C

4

12

Possible Duplicate:
What is the difference between #import and #include in Objective-C?

What is the difference between

#include< >
#include" "

#import< >
#import" "
Clackmannan answered 6/11, 2012 at 11:40 Comment(1)
I believe, in Objective C? Check this: #440162Auriscope
W
16

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.

Wayfarer answered 6/11, 2012 at 11:43 Comment(0)
G
3

When using #import, the header include guard is unnecessary. Otherwise, it's just like #include.

The header include guard, seen in C and C++ files:

#ifndef HGUARD_MONHeader_h
#define HGUARD_MONHeader_h

...header contents...

#endif
Gaea answered 6/11, 2012 at 11:42 Comment(2)
So, it isn't like "import refers to a package file(like in java)" and "include refers to local class file"Clackmannan
@user1799919 nope, not like Java here. if you have properly include-guarded all of your headers, you could literally exchange #include and #import and the files used for discovery would be identical. they do not alter scope or order of search paths.Gaea
M
3

import is super set of include, it make sure file is included only once. this save you from recursive inclusion. about "" and <>. "" search in local directory and <> is use for system files.

Makhachkala answered 6/11, 2012 at 11:49 Comment(1)
Hmm thanks i understood the difference between "" and <>. But, import and include still confuses.Clackmannan
B
2

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.

What is the difference between #import and #include in Objective-C? :

#include and #import request that the preprocessor* read a file and add it to its output. The difference between #include and #import is that

#include allow you to include the same file many times. #import ensures that the preprocessor only includes a file once. C programmers tend to use #include. Objective-C programmers tend to use #import.

* Compiling a file in Objective-C is done in two passes. First,
the preprocessor runs through the file. The output from the preprocessor goes into the real compiler.

Burgher answered 6/11, 2012 at 11:43 Comment(3)
-1 you shouldn't copy/paste other people's answers. especially if not referenced. the source: #440162Gaea
may your information every thing avialable is online, and you have whatever knlowledge is also from lerning from book, web ..But main thing is how can you help to person ..Burgher
a) very wrong b) also very wrong c) helping people is good, but there are more acceptable ways to do so -- and i figure you already knew this.Gaea

© 2022 - 2024 — McMap. All rights reserved.