[swift-users] C/FFI interop

Eric Wing ewmailing at gmail.com
Wed Dec 23 21:38:45 CST 2015

On 12/23/15, Thomas Catterall via swift-users <swift-users at swift.org> wrote:
> Indeed I was - I'm quite sorry for the confusion, I didn't make my intent
> clear as I should have, such as through example. What I'm referring to is,
> for instance, offering a way for a Ruby library to interface with a module
> written in Swift to communicate with Redis (as an example, not something I'm
> thinking too seriously about).
> Sent from my iPhone
>> On 23 Dec 2015, at 19:20, Jason Dusek <jason.dusek at gmail.com> wrote:
>> I think the OP was asking not about importing C symbols into Swift but
>> rather exporting Swift to C.

I am also interested in this same general question, but maybe a little
simpler. I simply would like to call Swift from C.

My use case is much simpler though. I have a lot of code written in C,
often at pretty low level and/or platform level. It may not be easy to
rewrite this in pure Swift. But higher-level functionality could be
more easily written in Swift, so it would be nice to call out to Swift
from C.

One specific example I'm thinking of is Android and the NDK. On
Android, everything starts in Java. You can cross into C via the NDK.
But once in C, you still need a way to reach Swift.

On Apple platforms, I achieved this by going through Objective-C
(pre-Xcode 7). I created Swift classes that could be called from
Obj-C. Then from C, I crossed into Obj-C, and then finally Swift. But
on Swift on Linux doesn't seem to come with Obj-C. Furthermore, I
noticed that with Xcode 7+, my little C/Obj-C/Swift trampolines don't
seem to work any more unless I make my Swift classes full subclasses
of NSObject, which I was trying to avoid because I expected Linux
Obj-C support to be extremely minimal even if it were available.


More information about the swift-users mailing list