Xcode 7 静态库新链接规则

XcodeiBcker • 于 2015-10-13 09:46:41 +0800 • 最后由 hugrup2015-10-20 07:38:00 +0800 5381 阅读

有人升级Xcode7后遇到静态库链接错误,以前在命令行编译的静态库无法直接链接了,很好奇所以跟进一下并把结果记录下来

报错如下: ld: in xxx.a(xxx.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)

按理来说,模拟器和OSX用的应该是同一份architecture才对,为什么会出现此类报错呢,于是乎进行尝试,新建测试文件Test.c

```

include

void hi(){ printf("helloworld"); } ```

终端里用clang编译并生成静态库

$ clang -c -arch i386 -arch x86_64 Test.c $ libtool -static -o Test.a Test.o

接下来拖到Xcode中尝试编译工程,果然报错: ld: in /tmp/Test.a(Test.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)

顺着错误找到了apple论坛上已经有人翻出了答案:

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 compiler options.

You can see which platform an object file was built for by running “otool -lv” on the .a (or .o) file. and look for LCVERSION. You should see LCVERSIONMINIPHONEOS, LCVERSIONMINMACOSX, LCVERSIONMINWATCHOS, or LCVERSIONMIN_TVOS.

Besides now longer being able to mix OSX intel binaries and simulator binaries, this means going forward, you need to build different libraries for the iOS, watchOS, and tvOS simulators.

于是乎尝试编译时加上-miphoneos-version-min=7.0 $ clang -c -arch i386 -arch x86_64 Test.c -miphoneos-version-min=7.0 $ libtool -static -o Test.a Test.o 重新推进Xcode里,编译通过,问题解决

回复: 1
  • hugrup 2015-10-20 07:38:00 +0800

    原来苹果还埋了这个标志位···

  • 请注意单词拼写,以及中英文排版,参考此页
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
  • 支持表情,见 Emoji cheat sheet
  • @name 会链接到用户页面,并会通知他
  • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif
Ctrl+Enter