1. 程式人生 > >編譯報錯framework not found Metal for architecture armv7 問題

編譯報錯framework not found Metal for architecture armv7 問題

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

HoweverCOREVIDEO_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!) –

大概意思就是說:

有三種情況會出現這個報錯,第三種那個應該是蘋果的bug,因為xcode6一起下載的sdk包裡面一些標頭檔案是xocde5裡面sdk找不到的,xcode6沒有向前相容xcode5,所以如果你在xcode6下建立的專案,拿到xcode5下面有可能會報錯。