Search code examples
swiftbuildopen-sourcebuild-processld

Linking failure in open-source Swift project


I've been following the "Getting Started" tutorial on http://swift.org. Upon creating new Swift "Hello World" project, I ran shell command:

$ 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.

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

Solution

  • 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. 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. 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
    2. link `libobjc.dylib - that worked

    Here are the shell commands I used (I did the same thing for libSystem because it required the same treatment):

    $ 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. However, I don't believe that is user-friendly installation process, Apple.