Xcode7编译遇到building for iOS simulator, but linking in object file built for OSX, for architectu问题解决方案

Xcode 7's New Linker Rules

The other day I received a new error from Xcode 7. I was building an iOS project for simulator, linking in a fat static library with i386 and x86_64. The error looked like this:

ld: in /Users/tk/Code/.../libtest.a(TestLib.o), building for iOS simulator,
    but linking in object file built for OSX, for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

This is a pretty strange thing for a linker to say. The iOS simulator and OSX use exactly the same architectures. How is it supposed to know the difference, and why does it matter?

I followed this up with some testing. If I compiled a simple C library like this:

$ clang -c -arch i386 -arch x86_64 TestLib.c
$ libtool -static -o libtest.a TestLib.o
$ lipo -info libtest.a
Architectures in the fat file: libtest.a are: i386 x86_64

…then that would fail to link inside an iOS simulator project.

But if I took exactly the same files and built a static library using Xcode, then thatlibtest.a would link fine, even though both files contained the same function for the same architectures.

So Xcode was doing something magic and I figured it must be a bug in Xcode 7’s linker. At this point I filed a bug report with Apple, who quickly got back to me with this explanation:

"In Xcode 7, the linker enforces that all files linked together were built for the same platform. You control the platform via -mmacosx-version-min=XX or -miphoneos-version-min=XX."

As far as I can tell Xcode 7 makes sure that every single object file is marked with extra flags for both the platform (OSX, iOS, tvOS, watchOS) and the minimum version of the platform that it’s designed for. I’m not sure why. Perhaps there are technical reasons, but I can certainly imagine that with so many platforms flying about this could be a source of errors.

I tried recompiling my static library using clang -miphoneos-version-min=7.0and sure it enough it worked. Makes sense. (Though confusingly, this particular option doesn’t appear in clang --help.)

They went on to explain that you can see these flags in .o or .a files by using theotool command. I tried it out and it came out like this:

# A static library marked as iOS
$ otool -lv libtest.a | grep LC_VERSION
      cmd LC_VERSION_MIN_IPHONEOS
      
# A static library marked as OSX
$ otool -lv libtest.a | grep LC_VERSION
      cmd LC_VERSION_MIN_MACOSX

Bafflingly, I haven’t been able to find any Apple documentation about these clang flags and their new significance. I decided I’d better write this up as it might save some people some time.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangyang6275

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值