What Is Prefix.Pch File in Xcode

What is Prefix.pch file in Xcode?


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?

PCH File in Xcode 6

Add the pch file which is under Others in File-New...and don't forget to add it to your LLVM6.0 - Language section of Build Settings as Project/whateveryounamedyourpchfile.pch

Why isn't ProjectName-Prefix.pch created automatically in Xcode 6?

I suspect because of modules, which remove the need for the #import <Cocoa/Cocoa.h>.

As to where to put code that you would put in a prefix header, there is no code you should put in a prefix header. Put your imports into the files that need them. Put your definitions into their own files. Put your macros...nowhere. Stop writing macros unless there is no other way (such as when you need __FILE__). If you do need macros, put them in a header and include it.

The prefix header was necessary for things that are huge and used by nearly everything in the whole system (like Foundation.h). If you have something that huge and ubiquitous, you should rethink your architecture. Prefix headers make code reuse hard, and introduce subtle build problems if any of the files listed can change. Avoid them until you have a serious build time problem that you can demonstrate is dramatically improved with a prefix header.

In that case you can create one and pass it into clang, but it's incredibly rare that it's a good idea.


EDIT: To your specific question about a HUD you use in all your view controllers, yes, you should absolutely import it into every view controller that actually uses it. This makes the dependencies clear. When you reuse your view controller in a new project (which is common if you build your controllers well), you will immediately know what it requires. This is especially important for categories, which can make code very hard to reuse if they're implicit.

The PCH file isn't there to get rid of listing dependencies. You should still import UIKit.h or Foundation.h as needed, as the Xcode templates do. The reason for the PCH is to improve build times when dealing with really massive headers (like in UIKit).

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

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.

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.

.pch File in Xcode 6.1


  1. You'll need to create a PCH file named ProjectName-Prefix.pch. In the new file dialog you'll find PCH under Other.
  2. Go to Project > Build Settings and search "Prefix Header". Under "Apple LLVM 6.0 - Language" add your PCH file: ProjectName/ProjectName-Prefix.pch note: make sure you select 'All' not 'Basic'
  3. Now add any headers you want globally included in your PCH file
  4. Clean and rebuild your project.

PrefixHeader.pch file can't find headers

Select your project (named "Project" for example) and click on "ProjectTests" (in TARGETS section).
Then, go to Build Phases > Link Binary With Libraries and add Parse.framework

iOS Prefix.pch best practices

Ewww… 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.

Iphone write code in prefix.pch

Pre-compiled header files were brought to serve one purpose: to make compiling faster. It is compiled and stored in cache, and automatically included in every source file during the compilation time. Its like each source file does,

#import "Prefix.h"

This can be handy for project-wide #defines. (FYI, #defines are a code smell)

Xcode quotes: Precompiling the prefix header will be most effective if the contents of the prefix header or any file it includes change rarely. If the contents of the prefix header or any file it includes change frequently, there may be a negative impact to overall build time.

More clear explanation is here

Please keep this in mind when you #import s source file header in .pch. I would suggest you to explore other ways to write your code rather than choosing .pch file.

You can use Prefix.h for #import of constants and utility source files. Also for convenience in debugging like this:

#ifndef DEBUG
#define NSLog(x,...)
#endif

I see that you want to declare constant strings to use project-wide. Create a new header file "Constants.h"(or "Global.h", as you like it) and write all your global constants(usually macros & typedef enum's) here. However, to declare constant strings using extern you would need implementation file too.

In "Constants.h",

extern NSString *const app_ID;

And in "Constants.m",

NSString *const app_ID=@"dfgdf";

Hope that helps.



Related Topics



Leave a reply



Submit