Error when compiling a static library using Swift

2019-02-02 02:55发布

问题:

This question already has an answer here:

  • Static Library and Swift 3 answers

Seeing this error when adding a swift file for compilation into a static library. using XCode 6.0 (6A215l)

Full libtool error: Libtool DerivedData/SwiftTest/Build/Products/Debug-iphonesimulator/libstatic.a normal i386 cd /Users/al/dev/ios/SwiftTest2 export IPHONEOS_DEPLOYMENT_TARGET=8.0 export PATH="/Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode6-Beta.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin" /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool -static -arch_only i386 -syslibroot /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk -L/Users/al/dev/ios/SwiftTest2/DerivedData/SwiftTest/Build/Products/Debug-iphonesimulator -filelist /Users/al/dev/ios/SwiftTest2/DerivedData/SwiftTest/Build/Intermediates/SwiftTest.build/Debug-iphonesimulator/static.build/Objects-normal/i386/static.LinkFileList -ObjC -L/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator -Xlinker -rpath -Xlinker /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator -Xlinker -force_load -Xlinker /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_iphonesimulator.a -Xlinker -sectalign -Xlinker __SWIFT -Xlinker __ast -Xlinker 4 -Xlinker -sectcreate -Xlinker __SWIFT -Xlinker __ast -Xlinker /Users/al/dev/ios/SwiftTest2/DerivedData/SwiftTest/Build/Intermediates/SwiftTest.build/Debug-iphonesimulator/static.build/Objects-normal/i386/static.swiftmodule -o /Users/al/dev/ios/SwiftTest2/DerivedData/SwiftTest/Build/Products/Debug-iphonesimulator/libstatic.a

I've create a sample project here, the error happens in a completly fresh project: https://github.com/amleszk/SwiftTest/tree/cd94ca21d817fed336b1a3bfc774a13608e0d1ca

To reproduce checkout the project and build the SwiftTest target, which has a dependency on the static library. Any help appreciated

回答1:

Update: It seems like the official release of Xcode 6 still has that same issue.

From the xcode 6 beta 2 release notes:

It is not possible to build static libraries which contain Swift code in this release



回答2:

I have found the solution. This is hook, but it works. You should do this as root.

  1. Go to default toolchain bin directory

    cd $(xcode-select -p)/Toolchains/XcodeDefault.xctoolchain/usr/bin

  2. Rename current libtool to old_libtool

    mv libtool old_libtool

  3. Create new libtool

    touch libtool && chmod +x libtool

  4. Insert current script as content of new libtool

    #!/usr/bin/python
    import os
    import subprocess
    import sys
    args=[]
    for arg in sys.argv[1:]:
        if arg != '-Xlinker' and arg != '-add_ast_path' and not arg.endswith('swiftmodule'):
            args.append(arg)
    args.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), 'old_libtool'))
    subprocess.check_call(args)
    
  5. Compile.



标签: ios swift ios8