简体   繁体   English

开源Swift项目中的链接失败

[英]Linking failure in open-source Swift project

I've been following the "Getting Started" tutorial on http://swift.org . 我一直在关注http://swift.org上的“入门”教程。 Upon creating new Swift "Hello World" project, I ran shell command: 创建新的Swift“ Hello World”项目后,我运行了shell命令:

$ swift build

and got the following output: 并得到以下输出:

Compiling Swift Module 'MyProject' (1 sources)
Linking MyProject
ld: library not found for -lobjc
<unknown>:0: error: build had 1 command failures
error: exit(1): /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-02-25-a.xctoolchain/usr/bin/swift-build-tool -f /Users/petrmanek/Projekty/MyProject/.build/debug.yaml default

I'm assuming that ld: library not found for -lobjc means that the linker can't find the Objective-C standard library, however I find that hard to believe as both files /usr/lib/libobjc.A.dylib and /usr/lib/libobjc.dylib are present on my file system. ld: library not found for -lobjc意味着链接器找不到Objective-C标准库,但是我很难相信,因为文件/usr/lib/libobjc.A.dylib/usr/lib/libobjc.dylib存在于我的文件系统上。

What do I do now? 现在我该怎么做?

My configuration is: 我的配置是:

Hardware: Mac mini (Late 2012)
OS: Mac OS 10.11 El Capitan

uname -a
    Darwin tywin 15.3.0 Darwin Kernel Version 15.3.0: Thu Dec 10 18:40:58 PST 2015; root:xnu-3248.30.4~1/RELEASE_X86_64 x86_64

swift --version
    Apple Swift version 3.0-dev (LLVM b361b0fc05, Clang 11493b0f62, Swift fc261045a5)
    Target: x86_64-apple-macosx10.9

I think I have solved it. 我想我已经解决了。 Here's my solution if anyone's interested. 如果有人感兴趣,这是我的解决方案。

Looking at the swift-build --help option list, I have discovered the option -Xlinker which allows me to specify flags directly for ld . 查看swift-build --help选项列表,我发现了-Xlinker选项,该选项使我可以直接为ld指定标志。 I used this option to tell it to be more verbose with command: 我用这个选项告诉它命令更加详细:

$ swift build -Xlinker -v

The output was: 输出为:

Linking MyProject
@(#)PROGRAM:ld  PROJECT:ld64-242
configured to support archs: i386 x86_64 x86_64h armv6 armv7 armv7s armv7m armv7k arm64
Library search paths:
    /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-02-25-a.xctoolchain/usr/lib/swift/macosx
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/lib
Framework search paths:
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/
ld: library not found for -lobjc
<unknown>:0: error: build had 1 command failures
error: exit(1): /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-02-25-a.xctoolchain/usr/bin/swift-build-tool -f /Users/petrmanek/Projekty/MyProject/.build/debug.yaml default

This was quite messy but we can see that /usr/lib is not among the library search paths. 这很混乱,但是我们可以看到/usr/lib不在库搜索路径中。 I had two options: 我有两个选择:

  1. add /usr/lib as a search path - that didn't work because ld strives to add /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/ prefix in front of every search path I add using the -L flag 添加/usr/lib作为搜索路径-该操作不起作用,因为ld努力在前面添加/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/前缀使用-L标志添加的每个搜索路径
  2. link `libobjc.dylib - that worked 链接`libobjc.dylib- 起作用了

Here are the shell commands I used (I did the same thing for libSystem because it required the same treatment): 这是我使用的shell命令(我对libSystem做了同样的事情,因为它需要相同的处理):

$ cd /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-02-25-a.xctoolchain/usr/lib/swift/macosx
$ sudo ln -s /usr/lib/libobjc.dylib 
$ sudo ln -s /usr/lib/libSystem.dylib 

The swift build command is working now and the product runs correctly. swift build命令现在正在运行,产品可以正常运行。 However, I don't believe that is user-friendly installation process, Apple. 但是,我认为这不是用户友好的安装过程,Apple。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM