1

You're trying to use precompiled headers to "speed up compilation". Xcode has a file called YourProject_Prefix.pch. You can include any number of header files in there that you like.

My question is, how do you select what header files should make it into your PCH? Should you just throw all your header files into there, or will that actually not be optimal?

Community
  • 1
  • 1
bobobobo
  • 64,917
  • 62
  • 258
  • 363
  • If you are using Xcode 5 - have a look at modules, which are supported for System frameworks. You can then do something like `@import UIKit;` and it will use a precompiled database rather than an actual text import. You can do this in individual files which is just as fast as precompiling and doesn't add the import globally. As for your pch file, add headers that are unlikely to change. Because if they do change the pch is precompiled again and you lose the benefits of it. – Abizern Oct 29 '13 at 15:23

2 Answers2

1

Just import your header files there like the ones below.

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

Pre-compiled headers, especially during building your app, can be very useful. The headers in the .pch file are only compiled the first time and then only if the headers change in the future. If your app imports many frameworks/headers that do not change; this could accelerate building (compiling) since the compiler will use the .pch versus compiling every imported framework/class every time you compile.

Nikos M.
  • 13,685
  • 4
  • 47
  • 61
  • Yes but, _which files should make the cut_? All of them? – bobobobo Oct 29 '13 at 15:14
  • Edited my answer, hope I helped you more. – Nikos M. Oct 29 '13 at 15:19
  • Well yes it appears there is a trade off. The more you include in the pch, the faster initial compilation is, but at a certain point, the final build time takes much longer. I don't know if I need more RAM or what, but something slows down the compilation process overall (by about 10 seconds or so) when I include too many files in the PCH. – bobobobo Nov 03 '13 at 19:49
1

the pch file will be included in all your source files by default.

that means you should really only put header files in there that are more or less global or never change. I believe putting all your headers in there would slow down compilation because every time you changed one it would cause every other file in your project to have to recompile. (I did not test or research this)

here is a sample from one of my projects:

#ifdef __OBJC__
    #import <Cocoa/Cocoa.h>
    #import "Errors.h"
    #import "Localization.h"
    #import "Logging.h"
#endif

Additionally, take the linked comments about C++ with a grain of salt. C++ uses templates and such that go in header files and make compilation take much longer than you are going to see in objective-c. in objective-c you are only likely to have types and interfaces, not implementation in a header.

Brad Allred
  • 7,323
  • 1
  • 30
  • 49