What is Prefix.pch file in Xcode?

IosXcode

Ios Problem Overview


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

  • If i remove that Prefix.pch file 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.pch file

Ios Solutions


Solution 1 - Ios

Precompiled header.

What is it?

A Prefix.pch is 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.

Xcode

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

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.

https://stackoverflow.com/questions/4728225/is-it-ok-to-remove-prefix-pch-file-from-the-xcode-project

Solution 2 - Ios

What is Prefix.pch file?

A .pch is a Pre-Compiled Header.

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.

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

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.

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.

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

Solution 3 - Ios

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 .pch file naming formation is yourProjectName-Prefix.pch

Solution 4 - Ios

Prefix Headers and Precompiled Headers (.pch)

History:

#include => #import => Precompiled Headers .pch => @import Module(ObjC); => import Module(Swift)

[#include vs #import]

[@import Module(ObjC);]
[import Module(Swift)]

Precompiled Headers - prefix.pch

To create new .pch file

File -> New -> File... -> PCH File

PrefixHeader.pch sample:

#ifndef PrefixHeader_pch
#define PrefixHeader_pch
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
#endif

From Xcode 6 you should set .pch manually. Previously it was called <product_name>-Prefix.pch

Xcode:

https://i.stack.imgur.com/hdBLx.png" height="50" />

Prefix Header(GCC_PREFIX_HEADER) - path to .pch. Adds all content from .pch to all source files

SomeFile1.h

//You can skip this section. It will be compiled 
//#import <UIKit/UIKit.h>
//#import <Foundation/Foundation.h>

//implicitly next line is used
#import "PrefixHeader.pch"

Precompile Prefix Header(GCC_PRECOMPILE_PREFIX_HEADER) - When Yes then Prefix headers from GCC_PREFIX_HEADER will be used once* to generate precompiled sources and will be stored in a cache which speeds up a build time

//path to cached precompiled headers
/<some_path>/DerivedData/ExperiementsModuleSwift-dsetoksxykdmgvczgtnmaqmjwhzk/Build/Intermediates.noindex/PrecompiledHeaders/SharedPrecompiledHeaders/14385052922615550324
//e.g
/Users/alex/Library/Developer/Xcode/DerivedData/ExperiementsModuleSwift-dsetoksxykdmgvczgtnmaqmjwhzk/Build/Intermediates.noindex/PrecompiledHeaders/SharedPrecompiledHeaders/14385052922615550324

https://i.stack.imgur.com/MeihE.png" height="200" />

#import vs .pch

#import has a disadvantage - slow build time.

Precompile Header - improve build time because precompile prefix headers(global). When some content of prefix headers is changed it means that Compiles must recompile that again. that is why you should use stable(not often changeable) prefix headers here or you dont't see build time improvement. Also you should not use abuse it because this work is dome implicitly and it hard to support such system

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSoumya RanjanView Question on Stackoverflow
Solution 1 - IosRafa de KingView Answer on Stackoverflow
Solution 2 - IosHemangView Answer on Stackoverflow
Solution 3 - IosiPatelView Answer on Stackoverflow
Solution 4 - IosyoAlex5View Answer on Stackoverflow