This question already has an answer here:
- Static Library and Swift 3 answers
I've been experimenting with porting an Obj-C library to Swift, and I've run into a problem where the linker fails to build a static library when Swift code is in the project.
As a minimal reproducer, go into XCode6 and create a new iOS Static Library. It'll give you a project with a blank .h and .m file. This will compile fine. Then, add a new .swift file to the project (with or without creating a header bridge). This too should compile fine, but instead it fails during linking:
Libtool /Users/alexkarantza/Library/Developer/Xcode/DerivedData/Test-alenfoymgkewlghfjjvizjjuvign/Build/Products/Debug-iphonesimulator/libTest.a normal i386
cd /Users/alexkarantza/Workspace/Test
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/alexkarantza/Library/Developer/Xcode/DerivedData/Test-alenfoymgkewlghfjjvizjjuvign/Build/Products/Debug-iphonesimulator -filelist /Users/alexkarantza/Library/Developer/Xcode/DerivedData/Test-alenfoymgkewlghfjjvizjjuvign/Build/Intermediates/Test.build/Debug-iphonesimulator/Test.build/Objects-normal/i386/Test.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/alexkarantza/Library/Developer/Xcode/DerivedData/Test-alenfoymgkewlghfjjvizjjuvign/Build/Intermediates/Test.build/Debug-iphonesimulator/Test.build/Objects-normal/i386/Test.swiftmodule -o /Users/alexkarantza/Library/Developer/Xcode/DerivedData/Test-alenfoymgkewlghfjjvizjjuvign/Build/Products/Debug-iphonesimulator/libTest.a
error: /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: unknown option character `X' in: -Xlinker
Usage: /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool -static [-] file [...] [-filelist listfile[,dirname]] [-arch_only arch] [-sacLT] [-no_warning_for_no_symbols]
Usage: /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool -dynamic [-] file [...] [-filelist listfile[,dirname]] [-arch_only arch] [-o output] [-install_name name] [-compatibility_version #] [-current_version #] [-seg1addr 0x#] [-segs_read_only_addr 0x#] [-segs_read_write_addr 0x#] [-seg_addr_table <filename>] [-seg_addr_table_filename <file_system_path>] [-all_load] [-noall_load]
This happens if I'm targeting the simulator or the device. It looks like perhaps having the Swift file in the project is causing it to use linker options normally reserved for executables, even though the target is a static library? I don't know enough about the build options to know if this is a bug in the beta, or some option I should be configuring. It seems questionable that the build would legitimately fail on such a trivial example. Any thoughts?
To answer my own question after some discussion in the comments, it is simply not possible to build static libraries that include Swift code yet. I'm writing this as of Beta 4, and the release notes still say, under "Known issues in Xcode 6 beta 4":
Xcode does not support building static libraries that include Swift code. (17181019)
I came across same problem and I did found a solution for it. 'libtool' is failing because of -Xlinker attribute, which tries to specify Swift module - that seems not to be supported for static libraries. (I also think that is mistake/deficiency by Apple, but maybe that's a discussion for another thread)
What I did is I copy/pasted the whole libtool command to terminal, remove all '-XLinker ...' parameters and surprisingly build succeeded from command line.
My static library project included both Objective-C and Swift sources and they were both 'packed' into produced static library!
The only downside was that Swift module was not produced but in my case that didn't matter because Swift was used only internally - I had only Objective C external interfaces.
I think it would be possible also to expose Swift 'interfaces' by copying produced static lib bridging header along the produced static lib.
I have been looking at this issue too for a while, here is what I found:
First, in Xcode 6 Beta, there is no language selection when you create Cocoa Touch Static Library, the language is set to Objective-C by default, though you can add Swift file to the project, it gives error like in your question as a result. My interpretation is that it is Apple's intention to steer away from creating Swift static library.
So If you intent to build a library that leverages power of Swift, use Cocoa Touch Framework instead of Static Library. I have written steps on creating framework project and app project, you can find it here. Note in that example, I created an Objective-C framework project. Choose Language Swift if want pure Swift framework project.
Also note If you look to import Swift framework into Objective-C project, or mix the languages, there is a good reading here Swift and Objective-C in the Same Project.
Hope this will give you a good direction.
Check out this blog (translation needed)
http://andelf.github.io/blog/2014/06/25/write-swift-module-with-swift-cont/
Look at this threat XCode5 simulator: unknown option character `X' in: -Xlinker