How to call C from Swift?

I'm writing a program that will use some C code and I need to call into it (just one C function, not a C++ class or library). I'm doing this on Linux so there is no Xcode. I'd like to avoid creating a package to abstract the C code because it's not a library that I'm calling into. What is the simplest way to call a C function from Swift? Can I just declare it as an external function in Swift? If I cannot declare it, do I need to pass to Swift from C a pointer to the function that the Swift code will call? Thanks.

Does this help?

Hi, no unfortunately that doesn't help because my Swift code doesn't see the C function.
I've tried using the typealias syntax, but that didn't work.
As I'm not using Xcode, any magic that it might be doing to make C functions visible to Swift isn't happening.

Is it in your bridging header? (I’ve never done this, so I’m just throwing resources I can find at you.)

You probably want to give swiftc the -import-objc-header flag. This header file (called a "bridging header" in the parlance) declares all the C resources you want to pull in.

1 Like

Amazing. It worked! Thank you.

1 Like