Tuesday, March 3, 2015

Causes of "Undefined Symbols" Errors in Xcode

When building in Xcode, you might encounter the following error:

Undefined symbols for architecture armv7

This error typically occurs due to one of three common reasons. Below, I’ve outlined the potential causes and solutions for each scenario.

  1. Missing Library Link This is the most common cause. It happens when a header is imported, but the corresponding library is not linked. This often occurs when libraries that are not included by default in the project have their headers added.
    Solution:
    Go to [Build Phases][Link Binary With Libraries] and add the appropriate library.

  2. Uncompiled Source Files This issue arises when files are copied into the project but not added to the compile sources.
    Solution:
    Go to [Build Phases][Compile Sources] and add the new .m files.

  3. Wrong Architecture for Linked Static Libraries Sometimes, you may add a static library compiled for the simulator’s architecture (e.g., i386) but forget to include the device’s architecture (e.g., armv7).
    Solution:
    If the library vendor provides libraries for different CPU architectures, make sure to include both the simulator library (e.g., i386) and the device library (e.g., armv7).

Detailed Explanation of Causes

  • Missing Required Library:
    If the header file is imported but the corresponding library is not linked, the compiler cannot find the necessary symbols. To resolve this, add the library to the [Link Binary With Libraries] section under Build Phases. If the library is located outside of the default search paths, add the folder containing the library to Library Search Paths under Build Settings and then include the -l{library_name} flag (excluding the lib prefix) in Other Linker Flags.

  • Uncompiled Source File:
    If you’ve added files to the project but haven’t included them in the Compile Sources section, the compiler won’t process those files. Add the .m files under [Compile Sources] in the Build Phases section.

  • Incompatible Architecture for Linked Libraries:
    If you’ve added a static library compiled for a different architecture (e.g., i386 for the simulator), it may not work when targeting a different architecture (e.g., armv7 for the device). Make sure that you include libraries for both the simulator and the device if the vendor provides them separately.