Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cost of Including Header Files in Objective-C

This may seem like a really stupid question, but what is the cost of including (actually, calling #import) a header file in Objective-C? I get tired of constantly including the same headers in various locations, so I decided to simply create a GlobalReferences.h file that includes several commonly-referenced headers.

Is there any appreciable cost for including references to other files if they aren't even used? My gut tells me "no" as it seems like the linker is just made aware of other files when using #import, but I wasn't sure if special considerations need to be taken for iPhone development, which is what my project concerns. Any thoughts?

like image 604
CIFilter Avatar asked Apr 22 '09 14:04

CIFilter


3 Answers

The linker knows nothing about #imported files. In fact, the Objective-C compiler knows nothing about them either, they are preprocessed out by the preprocessor. The preprocessor effectively inserts the contents of the headers into the point you've included them in your source file. The actual Objective-C compiler will then have to process extra function prototypes and class interface definitions even though they aren't being used. Though this is not usually a lengthy task, it can increase compile times. The resulting size and performance of your application should remain unaffected.

To see what the raw source code looks like (including all header files and expanded macros etc):

gcc -E your-source-file.m
like image 89
dreamlax Avatar answered Oct 28 '22 02:10

dreamlax


Importing/including more header files than you need will increase compile times. You can alleviate some of this pain with pre-compiled headers.

like image 44
Barry Wark Avatar answered Oct 28 '22 04:10

Barry Wark


The biggest drawback will be in compilation times. If all of your headers are imported in every source file, then the entire project will have to be rebuilt every time you make a change to a header file.

like image 36
e.James Avatar answered Oct 28 '22 04:10

e.James