Using Swift libraries in other languages


Swift has C interoperability, in a sense that you can call functions from C libraries in your Swift code.
How about the other way around, though?

Is it possible to make a Swift library that "pretends to be a C library" in order to call it from basically any other language? :thinking:

I know that Rust can do that (via the "no mangle" macro & auto-generated header files) & I was wondering if Swift can too.

If not, why? Because of Swift's runtime? Or because of the type-system? (guess that shouldn't be a problem if you only return "C types" like cString or "simple" values like integers & floats)

Also if not: could interoperability be achieved via other means? (from a user perspective - I don't plan on going hacking on the compiler :grinning:)


I've been googling & playing around with swiftc's options like -emit-library -static etc. but I got nowhere because I don't know what I'm doing tbh :joy:

Also I don't even know where to start learning about this stuff - from what I understand, every language "mangles" the identifier in some way & for the library to "look" like C to other language it needs to "mangle" the id's in "the C way" (hence Rust's "no mangle" macro)

EDIT no. 2:

Of course, I'm not the only one asking this question & it is possible... not stable though:

Hey @JK_Kross, did you try using the @_cdecl attribute as described in "Best way to call a Swift function from C?"?


This thread has a lot more information that may help you: Best way to call a Swift function from C?

EDIT: hah, Max beat me to it


Yeah, I discovered that (linked it in the edit) - do you know how "stable" it is?
I mean if it's like the "official way" to do it, or more like a "hack" :grinning: (underscored identifiers are internal & subject to change if I'm not mistaken)

If we stick to that vocabularry, it seems to be an "official hack". You're right, it's subject to change, but if it works then it's maybe worth trying. If any bugs are found, you'd probably be left to reporting and even fixing them yourself. At least if polishing the feature was a top priority for someone, I'd expect it to graduate into a full-fledge proposal or at least a pitch by this point. ¯\_(ツ)_/¯

We'll I'll definitely give it a go :grinning::+1:t2:

But I don't feel like learning C++ & how compilers work to try to implement that myself... :expressionless::expressionless::expressionless::stuck_out_tongue_winking_eye:

Thanks for your feedback!

That's exactly my feeling, but here I am, knee-deep in the C++ compiler's codebase, learning CMake and how the toolchain's build system works, just to implement a few things I wish that Swift had. See you on the other side :smiling_imp:

Off topic: What are you working on, may I ask? :wink:

It is support for WebAssembly in the Swift toochain and SDK, something that I started playing with back in 2018, but turned out to be a much bigger project than I ever expected.


Cool! Looking forward to see it, best of luck! :sunglasses::+1:t2:

1 Like
Terms of Service

Privacy Policy

Cookie Policy