Introduction
When transitioning to newer versions of development tools, such as moving from Xcode 11 to Xcode 12, developers often encounter challenges. One common issue is related to building projects for the iOS Simulator in Xcode 12 that previously compiled successfully in earlier versions. The core problem usually arises due to differences in architecture support between simulators and actual devices, especially with the introduction of Apple’s M1 chip.
This tutorial will guide you through understanding these architecture conflicts, specifically focusing on why errors related to "linking an object file built for iOS, for architecture ‘arm64’" occur. We’ll explore solutions using Xcode 12 to resolve these issues without compromising performance or compatibility.
Understanding the Problem
With Xcode 12, Apple introduced changes that align with their future plans for ARM-based Macs and devices. These include:
-
Support for arm64 in Simulators: Starting from Xcode 12, the iOS Simulator supports
arm64
architecture to simulate how apps would perform on Apple Silicon (M1) hardware. -
Removal of Valid Architectures Setting: Xcode 12 removed the option to specify "Valid Architectures" under build settings. This change affects projects that previously relied on this setting for compiling only specific architectures.
When building an iOS Simulator application in Xcode 12, developers may encounter errors if the project or its dependencies include binaries compiled specifically for arm64
architecture intended for actual devices rather than simulators. The error "building for iOS Simulator, but linking in object file built for iOS, for architecture ‘arm64’" indicates such a mismatch.
Solutions to Resolve Architecture Conflicts
1. Excluding arm64 from the Simulator Build Configuration
To prevent arm64
binaries from being included when building for simulators, you can exclude this architecture for simulator targets:
-
Using Xcode’s Build Settings:
- Navigate to your project’s Build Settings.
- Find or add an entry for Excluded Architectures.
- Set it as
arm64
specifically for the iOS Simulator SDK by adding:EXCLUDED_ARCHS[sdk=iphonesimulator*] = arm64
-
Using xcconfig Files: If you’re using custom
.xcconfig
files, add:EXCLUDED_ARCHS[sdk=iphonesimulator*] = arm64
-
Podfile Configuration:
For CocoaPods dependencies, ensure thatarm64
is excluded for simulator builds by adding a post-install script in your Podfile:post_install do |installer| installer.pods_project.build_configurations.each do |config| config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64" end end
2. Adjusting Build Active Architecture Only
Another approach involves adjusting the ONLY_ACTIVE_ARCH
setting:
-
Set ONLY_ACTIVE_ARCH to Yes:
- This ensures that only one architecture (the active architecture) is built at a time, preventing unnecessary builds for unsupported architectures.
- You can set this in both your project’s build settings and within Pod dependencies as follows:
spec.pod_target_xcconfig = { 'ONLY_ACTIVE_ARCH' => 'YES' }
-
Using post_install Script:
Modify the Podfile to enforceONLY_ACTIVE_ARCH
for all targets during pod installation:post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings["ONLY_ACTIVE_ARCH"] = "YES" end end end
Best Practices
-
Keep Dependencies Updated: Always ensure your dependencies are updated to support the latest architectures and Xcode features.
-
Test on Real Devices: Simulators provide a convenient testing environment, but they can’t fully replicate real device performance. Test on actual hardware whenever possible.
-
Stay Informed About Updates: Keep abreast of Apple’s development tools updates, as new releases may introduce or deprecate certain settings and configurations.
Conclusion
Navigating architecture-related issues in Xcode 12 requires an understanding of the changes introduced by Apple to support future ARM-based devices. By excluding arm64
from simulator builds and ensuring that your build configurations are appropriately set up, you can avoid common pitfalls and ensure a smooth development experience on Xcode 12.