2

I have a static library that compiles some C and ObjC code using the command line tools.

I'd like to start using Swift in this static library as it helps to simplify/shorten the code. (and well, it's the future right?)

I already searched and understood that it wasn't possible to include Swift files in a static library until I saw a post that said it was now possible with xCode 9 beta 4. (Static Library and Swift)

My questions are:

  • Is this actually true?
  • If it now works, can someone explain me how one should process to include Swift into a static library?
  • Is this feature extended to the command line tools, I mean can I do the same in command line?
  • If possible, then how should I do that? Or is there someone that can provide links on how to do it?

Note: I already explored the questions about how to call Swift from Objective-C from C. I know about the need of a Bridging Header and the generation of a project-Swift.h header file (along with some settings).

itMaxence
  • 1,230
  • 16
  • 28
  • just found some related post on apple developper forum: https://forums.developer.apple.com/thread/83706 https://forums.developer.apple.com/message/256569#256569 but I still can't figure out the way to go – itMaxence Nov 07 '17 at 15:09

1 Answers1

2

As far as using this feature in the command line tools I think it can be done but there are certain flags to be set. I have not done it personally but I found some good answers and info in this article.

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 have even 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).

Dean Kelly
  • 598
  • 7
  • 13