C中包含头文件的成本

C中包含头文件的成本

本文介绍了在Objective-C中包含头文件的成本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常愚蠢的问题,但是在Objective-C中包含(实际上调用 #import )头文件的成本是多少?我厌倦了在各个地方不断地包含相同的标题,所以我决定简单地创建一个包含几个常用引用标题的 GlobalReferences.h 文件。



如果没有使用其他文件的引用,是否有明显的成本?我的直觉告诉我不,因为链接器似乎在使用 #import 时才知道其他文件,但我不知道是否需要特别注意事项用于iPhone开发,这是我的项目所关心的。任何想法?

解决方案

链接器不知道 #import ed files 。事实上,Objective-C编译器对它们也不了解,它们由预处理器进行预处理。预处理程序有效地将头文件的内容插入到源文件中。实际的Objective-C编译器然后必须处理额外的函数原型和类接口定义,即使它们没有被使用。虽然这通常不是一项漫长的任务,但可以增加编译时间。您的应用程序的结果大小和性能应该不受影响。



查看原始源代码的外观(包括所有头文件和扩展宏等):

  gcc -E your-source-file.m 


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?

解决方案

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

这篇关于在Objective-C中包含头文件的成本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 08:09