如何摆脱警告“文件是为不支持的文件格式而构建的”与静态库链接时? [英] How to get rid of the warning "file was built for unsupported file format" when linking with a static library?

查看:120
本文介绍了如何摆脱警告“文件是为不支持的文件格式而构建的”与静态库链接时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其中包含我开发的外部库,每次使用设备作为目标进行编译时,我都会收到以下警告消息:

I've an application which includes an external library I developed, and I'm getting the following warning message every time I compile using the device as target:


mylib-release-iphonesimulator.a,文件
是为不支持的文件格式
构建的,这不是
链接的架构(armv7)。

mylib-release-iphonesimulator.a, file was built for unsupported file format which is not the architecture being linked (armv7).

我有两个版本的库,都添加到项目中。一个用于iphoneimulator,另一个用于iphoneos。

I've 2 versions of the library, both added into the project. One built for the iphonesimulator and the other for iphoneos.

即使它适用于任何目标(似乎编译器根据目标获取正确的库版本)那种警告变得很烦人。

Even though it works well on any target (seems the compiler takes the correct version of the library depending of the target) that sort of warning becomes anoying.

有没有办法摆脱警告,甚至更好地在一个库上编译两个平台,避免有两个相同的二进制文件库?

Is any way to get rid of the warning, or even better compile both platforms on a single library avoiding to have 2 binaries of the same library?

谢谢!

推荐答案

你不想得到摆脱这个错误,你想修复它。

You don't want to get rid of this error, you want to fix it.

这里的问题是你要链接将模拟器版本的库添加到应用的设备版本中。模拟器需要i386架构中的库,而设备需要armv6或armv7架构中的东西。

The problem here is that you're linking a simulator version of your library into the device build of your app. The simulator wants libraries in the i386 architecture, and the device wants things in the armv6 or armv7 architecture.

所以这里的解决方案是链接你库的正确版本。

So the solution here is to link the correct version of your library.

我通常做的是将它们组合成一个库,让链接器为我选择正确的版本。以下是您在终端中所做的事情:

What I usually do is combine them into a single library and let the linker pick the right version for me. Here's what you do in Terminal:

$ cd /path/to/my/libraries
$ ls 
  libMyLibrary-Device.a
  libMyLibrary-Simulator.a
$ file libMyLibrary-Device.a
  libMyLibrary-Device.a: Mach-O universal binary with 2 architectures
  libMyLibrary-Device.a (for architecture armv6):   current ar archive random library
  libMyLibrary-Device.a (for architecture armv7):   current ar archive random library
$ file libMyLibrary-Simulator.a
  libMyLibrary-Simulator.a: Mach-O universal binary with 1 architecture
  libMyLibrary-Simulator.a (for architecture i386): current ar archive random library
$ lipo -create -output libMyLibrary.a libMyLibrary-Device.a libMyLibrary-Simulator.a
$ ls
  libMyLibrary-Device.a
  libMyLibrary-Simulator.a
  libMyLibrary.a
$ file libMyLibrary.a
  libMyLibrary.a: Mach-O universal binary with 3 architectures
  libMyLibrary.a (for architecture armv6):  current ar archive random library
  libMyLibrary.a (for architecture armv7):  current ar archive random library
  libMyLibrary.a (for architecture i386):   current ar archive random library

然后您只需链接 libMyLibrary 而不是设备或模拟器版本,链接器将执行正确的事。

Then you just link libMyLibrary instead of the device or simulator version, and the linker will do the right thing.

这篇关于如何摆脱警告“文件是为不支持的文件格式而构建的”与静态库链接时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆