1

I want to create a universal static library, say called sampleStaticLib.a, which gets included in an app called HelloWorld. The HelloWorld app needs to use the APIs defined in the static library, and needs to work on both iOS simulator and iOS device. I understand from various posts in the Internet that I can accomplish this by using the lipo tool to aggregate the static libraries of both the simulator and device into a universal library, and then include the universal library inside the HelloWorld.xcodeproj.

However, alternatively, if I do the following:

  • simply set the valid architecture in the static library xcodeproject (sampleStaticLib.xcodeproj) to "armv7 armv7s arm64 i386 x86_64
  • generate the sampleStaticLibrary.a
  • include it in the HelloWorld.xcodeproj

My expectation is that, since I set the valid architecture of 'sampleStaticLib' to all architectures spanning x86 and ARM, the library should work on both simulator and device. But it doesn't work on the simulator.

So, can't setting a broad "valid architecture" replace the need to use 'lipo tool' while creating universal static libraries?

user777355
  • 41
  • 5

2 Answers2

1

No, unfortunately, that is not possible as trivially as you would like.

The reason is that when you build your project, it will build with the selected SDK for all of the requested architectures. The iOS SDK supports ARM, and the iOS Simulator SDK supports Intel. You need to build the ARM slices against the iOS SDK and the Intel slices against the iOS Simulator SDK and then lipo them together into a universal binary.

Jeremy Huddleston Sequoia
  • 22,938
  • 5
  • 78
  • 86
  • Thanks Jeremy. In that case, what does VALID_ARCHS = "armv7 armv7s arm64 i386 x86_64" mean in the iOS SDK? I see such a setting used by in several posts. How does 'i386' and 'x86_64' fit in this space then if these architectures are not even supported by iOS devices, as you can see in the link below? https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html#//apple_ref/doc/uid/TP40003931-CH3-SW85 – user777355 Dec 19 '14 at 21:21
  • This is the list that can be chosen from. Often a developer selects an architecture by choosing a device or simulator with the active scheme at the top-left of the project window. If the developer selects an iPhone5 device, then the active architecture is armv7. If an iPhone3GS is chosen, Xcode will give an error because armv6 is not in the list. – Walt Sellers Dec 19 '14 at 23:47
  • @WaltSellers: Why are the architectures "i386" and "x86_64" even added to this list if no iOS devices are ever going to run these? (at least until today).A default xcodeproject runs on a simulator even if the x86 architectures are not included in the "VALID_ARCHS". – user777355 Dec 20 '14 at 01:02
  • Xcode translates arm64 to x86_64 and armv7 to i386 when building for the sim. – Jeremy Huddleston Sequoia Dec 20 '14 at 01:03
0

It should work. For instance, this static library

https://github.com/cocos2d/cocos2d-x-3rd-party-libs-bin/blob/v3/png/prebuilt/ios/libpng.a

It has armv7, armv7s, arm64, i386 and x86_64 binaries.

$ file libpng.a 
libpng.a: Mach-O universal binary with 5 architectures
libpng.a (for architecture armv7):  current ar archive random library
libpng.a (for architecture armv7s): current ar archive random library
libpng.a (for architecture i386):   current ar archive random library
libpng.a (for architecture x86_64): current ar archive random library
libpng.a (for architecture cputype (16777228) cpusubtype (0)):  current ar archive random library <-- It's arm64

Xcode(Clang toolchain) can link this static library for iOS device and also iOS simulator. And no problems at all.

But it doesn't work on the simulator.

What do you mean? I recommend you to update the question about it more detail.

Kazuki Sakamoto
  • 13,929
  • 2
  • 34
  • 96