iphoneobjective-cdead-code

Are unused classes compiled by Xcode when building an app?


If something is not used in C++, it's not compiled at all. Is the same true for iPhone? If I compile a program and there are unused classes or other stuff, will it be compiled or ignored?


Solution

  • It depends on your build settings.

    Under your target build settings, then under "Linking" there is an option for "Dead Code Stripping". It defaults to yes for projects created with Xcode.

    enter image description here

    Here's a link to the build settings documentation from Apple (with an excerpt):

    https://developer.apple.com/library/archive/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html

    DEAD_CODE_STRIPPING (Dead Code Stripping)

    Description: Boolean value.

    Specifies whether dead code is stripped from the binary.

    Prerequisite: $GCC_DEBUGGING_SYMBOLS = full

    Values:

    YES: Dead code is stripped from the binary when the prerequisite is met.

    NO: Dead code is not stripped from the binary.

    Default value: NO Companions: “GCC_DEBUGGING_SYMBOLS (Level of Debug Symbols).” Prerequisite for: PRESERVE_DEAD_CODE_INITS_AND_TERMS

    Note, the docs say the value defaults to NO, but Xcode project settings default the value to YES.