What is Prefix.pch file in Xcode?

后端 未结 4 541
别那么骄傲
别那么骄傲 2020-12-02 06:50

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

  • If i

相关标签:
4条回答
  • 2020-12-02 07:03

    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

    0 讨论(0)
  • 2020-12-02 07:04

    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?

    0 讨论(0)
  • 2020-12-02 07:08

    History:

    #include => #import => .pch
    

    #include vs #import

    Precompiled Headers - prefix.pch

    #include vs @import has a disadvantage - slow build time because a compiler must parse and compile as many times as many .h files were imported.

    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.

    Let's take a look at Prefix.pch sample:

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

    To add .pch file go to Build Settings:

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

    The next step is @import[About]

    0 讨论(0)
  • 2020-12-02 07:25

    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

    0 讨论(0)
提交回复
热议问题