As far as I am aware, Swift calling conventions differ from C quite a lot. I am not a skilled C++ developer, but assuming that methods callable from C will be also callable from C++, there are two solutions I am aware of.
First solution is described in C reverse interoperabiity thread. Simply put @_cdecl("foo")
annotation before your public swift function in global scope. I also assume, that the _
character implies, that cdecl
annotation is not specified in Swift but merely a feature of the compiler which may not work at every instance, should be used with caution and may be subject to changes.
Also notice, that Swift's types, even the "simple" ones like structs, are not guaranteed to use C layout, so C structs can be used from Swift but Swift struct may not work in C.
Second solution is a Swift feature. You can create closure with @convention(c)
annotation. Such closure is safe to pass as a function pointer to C function. The tradeoff is, that capture lists are not available in such closures. Attributes — The Swift Programming Language (Swift 5.7)
Hope this helps.