ios Xcode 中的 Prefix.pch 文件是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23648593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:08:01  来源:igfitidea点击:

What is Prefix.pch file in Xcode?

iosxcode

提问by S R Nayak

So many developers are adding various convenience macros to the Prefix.pch. But my question is what is that Prefix.pchfile.

许多开发人员正在向Prefix.pch. 但我的问题是那个Prefix.pch文件是什么。

  • If i remove that Prefix.pchfile from my Xcode, then will my application run? Or will it show any error? Or will it crash during build?

  • How can i run my application without Prefix.pchfile

  • 如果我Prefix.pch从我的文件中删除该文件Xcode,那么我的应用程序会运行吗?或者它会显示任何错误?还是会在构建过程中崩溃?

  • 我如何在没有Prefix.pch文件的情况下运行我的应用程序

回答by RaffAl

Precompiled header.

预编译头。

What is it?

它是什么?

A Prefix.pchis a precompiled header. Precompiled headers were invented to make compiling faster. Rather than parsing the same header files over and over, these files get parsed once, ahead of time.

APrefix.pch是预编译的头文件。预编译头文件的发明是为了加快编译速度。这些文件不是一遍又一遍地解析相同的头文件,而是提前解析一次。

Xcode

Xcode

In Xcode, you add imports of the header files you want in a “prefix header,” and enabling Precompile Prefix Headerso they get precompiled. But the idea behind a prefix header is different from precompiling.

在 Xcode 中,您可以在“前缀头”中添加所需头文件的导入,并启用Precompile Prefix Header它们以便预编译。但是前缀头背后的想法与预编译不同。

A prefix header is implicitly included at the start of every source file. It's like each source file adds

每个源文件的开头都隐含地包含一个前缀头。就像每个源文件添加

#import "Prefix.pch"

at the top of the file, before anything else.

在文件的顶部,在其他任何东西之前。

Removing it.

删除它。

You can remove the precompiled header. This question has been already answered in thread I'm linking below. It contains all the information you need as well as useful comments.

您可以删除预编译的头文件。这个问题已经在我下面链接的线程中得到了回答。它包含您需要的所有信息以及有用的注释。

Is it OK to remove Prefix.pch file from the Xcode project?

可以从 Xcode 项目中删除 Prefix.pch 文件吗?

回答by Hemang

What is Prefix.pch file?

什么是 Prefix.pch 文件?

A .pch is a Pre-Compiled Header.

.pch 是预编译的头文件。

In the C and C++ programming languages, a header file is a file whose text may be automatically included in another source file by the C preprocessor, usually specified by the use of compiler directives in the source file.

在 C 和 C++ 编程语言中,头文件是一个文件,其文本可以被 C 预处理器自动包含在另一个源文件中,通常通过在源文件中使用编译器指令来指定。

Prefix headers are compiled and stored in a cache, and then automatically included in every file during compilation. This can speed up compilation, and lets you include a file without adding an import statement to every file using it. They are not required, and actually slow compilation whenever you change them.

前缀头被编译并存储在缓存中,然后在编译期间自动包含在每个文件中。这可以加快编译速度,并让您包含一个文件,而无需向使用它的每个文件添加导入语句。它们不是必需的,并且在您更改它们时实际上会减慢编译速度。

Yes, you can compile and run the project without .pch file

是的,您可以在没有 .pch 文件的情况下编译和运行项目

In Xcode, go to your target's build settings (Command-Option-E, build tab) and uncheck Precompile Prefix Header (GCC_PRECOMPILE_PREFIX_HEADER). You can also remove the value of the Prefix Header setting if you wish.

在 Xcode 中,转到目标的构建设置(Command-Option-E,构建选项卡)并取消选中 Precompile Prefix Header (GCC_PRECOMPILE_PREFIX_HEADER)。如果您愿意,您还可以删除 Prefix Header 设置的值。

Also note that,

还要注意的是,

Don't put macros in a.pch file! A .pch file is, by definition, a project specific precompiled header. It really shouldn't be used beyond the context of the project and it really shouldn't contain anything but #includes and #imports.

不要将宏放在 .pch 文件中!根据定义,.pch 文件是项目特定的预编译头文件。它真的不应该在项目的上下文之外使用,它真的不应该包含除#includes 和#imports 之外的任何内容。

If you have some macros and such that you want to share between headers, then stick 'em in a header file of their own — Common.h or whatever — and #include that at the beginning of the .pch

如果您有一些宏并且想要在头文件之间共享,那么将它们粘贴在它们自己的头文件中——Common.h 或其他——并在 .pch 的开头 #include

回答by iPatel

Prefix headers are compiled and stored in a cache, and then automatically included in every file during compilation. This can speed up compilation, and lets you include a file without adding an import statement to every file using it. They are not required, and actually slow compilation whenever you change them.

前缀头被编译并存储在缓存中,然后在编译期间自动包含在每个文件中。这可以加快编译速度,并让您包含一个文件,而无需向使用它的每个文件添加导入语句。它们不是必需的,并且在您更改它们时实际上会减慢编译速度。

Generally .pchfile naming formation is yourProjectName-Prefix.pch

一般.pch文件命名格式是yourProjectName-Prefix.pch

回答by yoAlex5

History:

历史:

#include => #import => .pch

#include vs #import

#include 与 #import

Precompiled Headers - prefix.pch

预编译头文件 - prefix.pch

#includevs @importhas a disadvantage - slowbuild time because a compiler must parse and compile as many times as many .hfiles were imported.

#includevs@import有一个缺点 -构建时间很慢,因为编译器必须解析和编译与.h导入的文件一样多的次数。

Precompiled header file - .pch- collects the common headers. When this file is precompiled ones it can be used in source files which speeds up a build time.

Precompiled header file - .pch- 收集公共标题。当此文件是预编译文件时,它可以用于加快构建时间的源文件。

Let's take a look at Prefix.pchsample:

让我们看一下Prefix.pch示例:

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

To add .pchfile go to Build Settings:

要添加.pch文件,请转到Build Settings

enter image description here

在此处输入图片说明

Developer has to manage .pchfile that imposes additional costs for supporting it.

开发人员必须管理.pch文件,这会产生额外的支持成本。

The next step is @import[About]

下一步是@import【关于】