0

I wonder the difference of import and include in Object-c

By the way, I am not clear about the difference of dynamic and static linking.

If I use a library with static linking, is that mean I copy the code i need from library for my program and link with them? Then my program can work with the code from library.

If i use a library with dynamic linking, is that mean I only reference the code i need from library to my program when my program is running. Then my program can work with the "reference code".

4

1 回答 1

2

#import vs. #include and static vs. dynamic linking are two completely unrelated topics.

#include includes the contents of a file directly in another file, and is available in C (and therefore also in Objective-C). However, it's common to want to include the contents of a file only if that file hasn't already been included. (You don't, for example, want to declare the same variables twice; it'd cause compiler errors!) That's why #import was added in Objective-C; it does exactly that: includes the contents of a file only if that file hasn't already been #imported. If you're not sure what to use, you should probably be using #import.

Static vs. dynamic linking is completely different--linking happens after compilation, so it couldn't possibly be related to #import and #include, which are part of the source code. Your thoughts on linking are exactly correct, however--statically linked libraries are included in your app, and your users don't need them. Dynamically linked libraries are referenced, and must be present on your users' machines for your app to run.

于 2012-06-26T16:45:07.217 回答