Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Library and Swift

So I'm working on an iOS project in Swift, and I wanted to create a Static library with some useful stuff in it.

My problem is when I try to build my lib in Xcode (version 6.3) I have a "Build Failed" followed by : /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: unknown option character 'X' in: -Xlinker

I've never saw this and it's not my first static lib. So I was thinking I may be linked to the fact that I'm using exclusively Swift class.

What do you guys think ? Thank you in advance.

like image 806
Que20 Avatar asked Apr 16 '15 13:04

Que20


People also ask

What is a static library in Swift?

A static library is a collection of compiled object files combined into a single library file, that may be linked into an app or framework target just like single object files are linked.

Does Swift support static libraries?

As mentioned, Apple does allow Swift in static libraries as of Xcode 9 Beta 4.

How do I create a static library in Swift?

Create Static Library ProjectOpen Xcode and select Cocoa Touch Static Library . Give it a name and select Swift as the development language. In our case, we will call it Networking and assume that it will contain the code to communicate with a back end. Press Cmd+N and select Swift file .

What is the difference between static and dynamic library in Swift?

statically linked modules are fastest to load (loading non-system dynamic frameworks is pretty expensive while system frameworks are optimized). When using static linking, all the symbols are within the same module, so the app start is fast. dynamically linked modules are slower to load, especially on iOS.


2 Answers

As mentioned, Apple does allow Swift in static libraries as of Xcode 9 Beta 4.

We attempted to do this on an existing project with an Objective-C-based target and "child" static library projects and kept running into a linking error

ld: library not found for -lswiftSwiftOnoneSupport for architecture x86_64 

also

ld: library not found for -lswiftDispatch for architecture x86_64 

This is because the main target (app) is trying to build solely against Objective-C and isn't told by the static library that it needs to include Swift libraries. This was because there weren't any Swift files in the Compile Sources section of our Build Phases for the app target.

So basically all you have to do is add at least one .swift file to that compile list and it will include the Swift libraries for you. It doesn't even need to have any code or values in it, it can be an empty file.

Then you can start adding Swift files to your "child" static library project. I would let it generate the bridging header for you at first then you can move it around and change what gets imported (make sure the project points to the right file in the build settings if you move it).

You should still keep in mind that using Swift and Objective-C within the same static library may have issues of its own. I suggest reading the Apple developer doc "Swift and Objective-C in the Same Project" on how to address importing Objective-C into Swift (using a bridging header) and how to use the Swift files in your Objective-C code (importing the generated -Swift.h for your library).

like image 93
Dean Kelly Avatar answered Sep 28 '22 19:09

Dean Kelly


Swift doesn't support static library

Although the correct way should be create a framework, there is a workaround here.

like image 40
Giordano Scalzo Avatar answered Sep 28 '22 20:09

Giordano Scalzo