开发者

How does #import search for files in Objective-C

I can only find a little bit of information on the topic. My understand right now is that

#import <my.h> // searches in the system paths
#import "my.h" // searches in the same dir as the source fil开发者_高级运维e - is that right?

I have an static library with .h files in a different location. Can I have the compiler search a new folder for .h files with #import "my.h" like I would in VC++


Your understanding is pretty much correct, except that #import "my.h" searches in the same directory as the source file, and all the system paths (the same ones searched by #import <my.h>).

I have an static library with .h files in a different location. Can I have the compiler search a new folder for .h files with #import "my.h" like I would in VC++

Yes. You have to pass the -I flag to gcc. For example, if you want to search path/to/my-other-dir, you'd pass -Ipath/to/my-other-dir to gcc. If you're using Xcode, you can configure this using the build options for your target; look for the option called Header Search Paths, under Search Paths.


In Objective-C, #import is just #include which will include each file once only. So it will follow #include rules in searching the file.

The <…> indicates system headers and "…" user headers. Which path the preprocessor will look into depend on the your settings. For GCC, <…> will look in:

 /usr/local/include
 <libdir/gcc/target/version>/include
 /usr/<target>/include
 /usr/include

and also the paths added by the -I and -F switches. "…" will look in current directory, and then those added by the -iquote switch, and then the system paths.

See http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html for detail.


Also of relevance

http://gcc.gnu.org/onlinedocs/cpp/Include-Syntax.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜