Is it possible to compile swift code to dynamic library?


(Zheng Ping) #1

I want to compile swift code to dynamic library, and I can link this
dynamic library to my C project. I don't know whether is it possible? Or
whether I can do this in the future?

···

--
with kind regards


(Slava Pestov) #2

Right now, we don’t have an ‘official’ way to expose Swift APIs to C. However there is the completely unsupported and subject to change or removal at any time @_cdecl attribute. Eg,

@_cdecl(“bar_foo") public func foo(x: Int) -> Int { return x }

This defines a function callable from C as:

extern int bar_foo(int x);

I don’t suggest using this for anything serious because it has some known problems and it’s going to change before it’s final (hence it’s undocumented, underscored status).

If someone wants a starter project, finishing off @_cdecl (and getting it through swift-evolution) would be really cool. My understanding is that the main thing lacking is untangling some of the diagnostics code to consider C and Objective-C separately, since C is more restrictive. We could even add support to PrintAsObjC for generating pure-C “bridging headers”. :slight_smile:

As for actually building a shared library, that depends on if you’re using Xcode or command line, Mac or Linux, etc. If you’re on the command line take a look at the -emit-library flag to the swiftc driver.

Slava

···

On Jan 9, 2017, at 5:57 PM, Zheng Ping via swift-users <swift-users@swift.org> wrote:

I want to compile swift code to dynamic library, and I can link this dynamic library to my C project. I don't know whether is it possible? Or whether I can do this in the future?

--
with kind regards
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users