开发者

@class vs. #import in header compile time saving with Clang?

I have read in a couple of places that it is advisable to use declarations like @class Something in header files and only importing these cl开发者_运维百科asses in the .m file to save compile time.

Is that really still necessary and makes compiling faster with LLVM Clang or was the compile time advantage only valid for older compilers like (old versions of) GCC?


Accidentally you can get circular import:

// A.h
#import "B.h"

// B.h
#import "A.h"

Preprocessor will include B.h in A.h, which in turn include A.h (because B.h imports it), which in turn import B.h again, etc. ad infinitum.

@class statements prevent that accidental error, since error caused by circular import is really REALLY unintuitive (speaking from personal experience and backtrace/error inspections).


@Eimantas is correct about the circular dependencies. It's also for performance. Imagine the case where you import A.h into B.h and B.h into C.m. Now, every time you modify A.h, C.m is recompiled, even though C.m may not rely on anything in A.h. Using @class avoids this kind of build churn. The move to clang doesn't change this.

Note that this only applies to headers you can change. It's generally fine and preferred to import system headers directly into you .h files.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜