Formalizing @cdecl

Hi Joe, a couple of times in this thread you speak about "importing a header". Is there a way to do this that doesn't involve creating a clang modulemap which imports the header? Because that works, but it always feels like jumping through hoops – creating multiple files, targets, build rules, to expose just one function, whose body should be defined in Swift anyway.

We are experiencing sporadic crashes calling functions exposed to the JNI via @_cdecl and we're looking for a "more official" way of doing it.

If you were to expose a function to C today in a non-broken way, how would you do it?

  1. Create thing.h which declares (in C syntax) that MyFunction exists
  2. Create a module map that imports thing.h
  3. Somehow import that module (SwiftPM, Xcode Build Settings, etc.)
  4. In the Swift code, something something (???) – how do we declare the function in Swift, and if we do, what was the point of importing the header?

I don't mean to derail the conversation with this. I am genuinely interested in finding out whether a use-case that I still somehow consider essential to this thread's (pre-)proposal can be achieved otherwise with reasonable effort.

2 Likes