Accessing C functions from Swift/SwiftUI

Hello,

I am building a sample/example iOS app with SwiftUI to demonstrate the integration of our text to speech library (university research team).

I have create a bridging header and added the line to import the .h file exposing the C type functions.

However, when I try the preview build, it falls over on the imported header.

At the top of the header is this :

#if defined(__APPLE__)
#define TTS_API extern "C" __attribute__((visibility("default")))
#else
#define TTS_API extern "C"
#endif

TTS_API void run_process(...)

The error message states :

Expected identifier or '('

At the beginning of the line holding the function definition (parameters removed for simplicity),with a reference to the macro expansion of TTS_API.

This macro (2nd line), however, is necessary for building the library for iOS.

What needs to be done in order to get the swift compiler to correctly handle this, please ?

Note that I am using XCode v12.2

It’s the extern "C" declaration. That's a C++ language feature, but the bridging header needs to be pure C (or Objective-C). If that code is in an external or shared header file then you can wrap it in an #ifdef __cplusplus guard. Or if it's directly in your bridging header in the project, you can just remove it.

Super, thank you Scott. That's just what I needed. Have a great day.

Accessing C functions from Swift/SwiftUI
 
 
Q