为什么Xcode模板有#imports重复Prefix.pch? [英] Why do Xcode templates have #imports that duplicate Prefix.pch?

查看:163
本文介绍了为什么Xcode模板有#imports重复Prefix.pch?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习iPhone编程时,我看到的每个Xcode模板都包含一个包含以下内容的AppName-Prefix.pch文件:

While learning iPhone programming, every Xcode template I've seen includes an AppName-Prefix.pch file with the following contents:

#ifdef __OBJC__
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
#endif



我理解这个文件的内容前缀到每个源代码文件。然而每个其他文件还导入UIKit,这似乎是多余的。例如, main.m 开始...

#import <UIKit/UIKit.h>

int main(int argc, char *argv[]) {
...

Mac OS X中的Cocoa应用程序执行相同的操作,在前缀文件和头文件中导入Cocoa.h。

Cocoa applications in Mac OS X do the same thing, importing Cocoa.h in both the prefix file and the header files.

?我从除前缀文件之外的所有源文件中删除了 #import 指令,并且它编译并正确运行。

Why have both? I removed the #import directives from all of the source files except the prefix file, and it compiled and ran correctly.


$


< b
$ b

这基本上是正确的,但是你需要理解这些微妙的点:从Xcode的每个编译最终都归结为 gcc clang 。 XCode的作用是首先编译 X.pch 文件:

That's basically correct but you need to understand the subtle points: Every compilation from Xcode eventually boils down to an invocation of gcc or clang. What XCode does is to compile the X.pch file first:

clang -x X.pch -o X.pch.gch

,然后当单个源文件 am )编译时,会发出

and then when an individual source file (say a.m) is compiled, it issues

clang -include X.pch a.m -o a.o

载入 pch 文件,触发使用预编译头。因此,从编译器的角度看, pch 文件的内容并不是自动添加的。相反,Xcode在调用编译器时将预编译头添加到文件中。

which loads the pch file, triggering the use of the precompiled header. So, from the point of view of the compiler, it's not really that the content of the pch file is automatically prefixed. Rather, Xcode prefixes the precompiled header to a file when it invokes the compiler.

XCode的未来版本可能停止这样做。因此,最好在 .m .h 中保留 #import code>文件。

A future version of XCode might just stop doing it. So, it's better to keep #imports in your .m or .h files, too.

你也可以这样想:使用 pch 文件就是Xcode为我们幕后的加速编译过程。所以,我们不应该以它基本上依赖它的方式编写代码,就像不从 .m 导入 UIKit.h c $ c> / .h 文件。

You can also think of it this way: the use of the pch file is just what Xcode does for us behind the scenes to speed up the compilation process. So, we shouldn't write codes in a way it essentially depends on it, like not importing UIKit.h file from our .m/.h files.

(另外,XCode4的语法着色如果您没有从 .h .m 文件中正确导入适当的头文件。)

(Also, it seems to me that XCode4's syntax coloring gets confused if you don't import the appropriate header files correctly from the .h and .m files.)

这篇关于为什么Xcode模板有#imports重复Prefix.pch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆