Ever since Apple introduced iOS SDK 8.0, apps referencing CoreVideo or OpenGLES may encounter the following linker error:
framework not found Metal for architecture armv7
This happens only when:
1. The build target is a real device and not the Simulator.
2. The linker flags include the “-ObjC” flag (this one is set by default in CocoaPods).
3. The code was compiled with SDK 8.0, and used by an app compiled for an earlier SDK (using XCode < 6.0).
The Reason
The reason behind this, is actually a bug / bad design by Apple, which failed to preserve backward compatibility for non-Metal devices (namely: armv7 & armv7s).
Taking a look at CoreVideo.h reveals this:
#if COREVIDEO_SUPPORTS_METAL
#include <CoreVideo/CVMetalTexture.h>
#include <CoreVideo/CVMetalTextureCache.h>
#endif
However, COREVIDEO_SUPPORTS_METAL is defined only as #if TARGET_OS_IPHONE
(which is not enough, since armv7/s devices do not support Metal).
The new “Link Frameworks Automatically” flag
In XCode 5, Apple introduced a new linker flag, named “Link Frameworks Automatically”, which defaults to “YES”. The purpose of this flag is to reduce the effort required by developers when adding a framework. Before the introduction of this flag, developers had to link their projects with system frameworks prior to using them. This flag allows them to simply use the frameworks.
After a thorough investigation, we found out that this works by the addition of a special load command in the output binary, called “LC_LINKER_OPTION”, which contains the framework that the binary links to. For example, when running:
otool -arch armv7 –l Appsee.framework/Appsee
We found out the following sections:
Load command 14
cmd LC_LINKER_OPTION
cmdsize 32
count 2
string #1 -framework
string #2 OpenGLES
Load command 15
cmd LC_LINKER_OPTION
cmdsize 32
count 2
string #1 -framework
string #2 Metal
Load command 16
As you can see, this will cause the linker to implicitly search for “Metal” when used.
The Solution
Until/if Apple addresses this issue, our solution was to disable the “Link Frameworks Automatically” flag when building the framework. This way, the load commands will not be included in the binary and it may be used for non-metal architectures. Nothing else should be affected, since this flag does not seem to work for frameworks referenced by our framework (i.e.: when an app links with Appsee, it would still need to reference the different frameworks which Appsee uses, whether the flag was on or off).
Happy linking!
The following posts helped us (thanks!) –
https://github.com/CocoaPods/CocoaPods/issues/2457
http://tonyarnold.com/2014/04/10/clean-up-your-projects-with-xcode-5.html
https://github.com/card-io/card.io-iOS-SDK/issues/66
大概意思就是说:
有三种情况会出现这个报错,第三种那个应该是苹果的bug,因为xcode6一起下载的sdk包里面一些头文件是xocde5里面sdk找不到的,xcode6没有向前兼容xcode5,所以如果你在xcode6下创建的项目,拿到xcode5下面有可能会报错。