Static Linking with Swift, XCode6-Beta
This question already has an answer here:
Static Library and Swift
- Google Drive SDK and iOS 5.1
- Did not understand process of initialize in swift programming
- Using multiple storyboards with a TabBarController
- Most efficient/realtime way to get pixel values from iOS camera feed in Swift
- SWrevealViewController without using navigationController
- What dpi resolution is used for an iPhone App?
5 Solutions Collect From Internet About “Static Linking with Swift, XCode6-Beta”
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 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.
Look at this threat XCode5 simulator: unknown option character `X' in: -Xlinker
In order to understand the error, you have to understand what the
command is attempting to do.
In this case it’s using Libtool, which is a slightly altered version
of libtool. There are some options that are specified in the command
line, but what we’re looking for is the destination file, and this is
passed in as the -o option, who’s argument is
Code.a, and the type of library we’re generating, and in this case
it’s -static. Both options together explicitly state that you’re
making a static library archive.
Because you’re making a static library archive, the only thing you’re
actually doing is taking .o files and possibly .a files and turning
them into another .a file. This can be roughly equated to the creating
of a .zip file from a set of files (.o), and the contents of other
.zip files (.a). There are very few things you can do while making
this archive, for example you can’t specify libraries that need to be
implicitly linked while building a static archive, you can’t specify
that you’re going to require entitlements.
libtool is complaining because it doesn’t understand the options that
are being used for a static library that are being passed in. In this
case the options are:
-Xlinker -sectcreate -Xlinker __TEXT -Xlinker __entitlements -Xlinker /Users/jr/ios/app/iCozi/build/iCozi.build/DevOnly-iphonesimulator/Cozi\
Common\ Code.build/Cozi\ Common\ Code.xcent
These are options that are present when you’re trying to link in an
entitlements file, which means that some options is specifying the use
of an entitlements file. In this case, you found the correct solution
yourself, which was to remove the entitlements file specified in the
project settings -> Code Signing -> Code Signing Entitlements ->
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.
- UIScrollView Paging Autolayout & Storyboard
- CloudKit – NSPredicate for finding all records that contain specified CKReference in a reference list
- Crash while using Datecomponents having larger differences
- Xcode: Dragging a Project to a Workspace shows tiny .xcodeproj file in Project Navigator. What's wrong?
- How to get title from WKInterfaceButton
- Disable Horizontal Scrolling to UITextView Programmatically
- Swift access to variable length array
- Launch Image names of iPhoneX?
- WatchKit SDK not retrieving data from NSUserDefaults
- A few IBOutlets pointing to nil
- How to upload images to a server in iOS with Swift?
- New image name for iPhone 5
- How to set x-axis on bar chart data – ios-charts?
- Add button to navigationbar programmatically
- Swift function as parameter within a class