打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
转载Xcode7编译遇到linking in object file built for OSX, for architecture x86

原文:http://karp.id.au/post/xcode_7_linker_rules/


Xcode 7's New Linker Rules

Tuesday, September 15, 2015

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_64clang: 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.aArchitectures 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.


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
使用XCode5和iOS7SDK出现Undefined symbols for architecture i386 clang: error: linker command failed with
ios 常见问题解决
Xcode工具静态检查分析代码
“undefined reference to” 问题汇总及解决方法
CMake脚本中如何修改XCode工程属性?
Xcode 升级后,常常遇到的遇到的警告、错误,解决方法 | iOS开发讨论区
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服