Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Prefix.pch file in Xcode?

Tags:

xcode

ios

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

like image 223
Soumya Ranjan Avatar asked May 14 '14 07:05

Soumya Ranjan


People also ask

How do I add PCH files to Xcode?

From your Xcode menu, select File > New > File... From iOS template options, select Other > PCH file. Name the file <target_name>-Prefix. pch, and then select Create.

How do I use PCH files in Swift?

In the Prefix Header field, add the path to the PCH file you just created $(PROJECT_DIR)/$(PROJECT_NAME)/(Project-Name)-Prefix. pch. In there, you will need to import the automatically generate Swift Bridging Header which will be named something like (Project_Name)-Swift. h.

What are .PCH files?

In computer programming, a precompiled header (PCH) is a (C or C++) header file that is compiled into an intermediate form that is faster to process for the compiler.

What is PCH file in Visual Studio?

When you create a new project in Visual Studio, a precompiled header file named pch. h is added to the project. (In Visual Studio 2017 and earlier, the file was called stdafx. h .) The purpose of the file is to speed up the build process.


2 Answers

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.

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

like image 71
Rafa de King Avatar answered Oct 20 '22 01:10

Rafa de King


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

like image 30
Hemang Avatar answered Oct 19 '22 23:10

Hemang