How to call C from Swift?

Is there a way to call C routines from Swift?

A lot of iOS / Apple libraries are C only and I'd still like to be able to call those.

For example, I'd like to be able to call the objc runtime libraries from swift.

In particular, how do you bridge iOS C headers?


Yes, you can of course interact with Apple's C libraries. Here is explained how.

Basically, the C types, C pointers, etc., are translated into Swift objects, for example a C int in Swift is a CInt.

I've built a tiny example, for another question, which can be used as a little explanation, on how to bridge between C and Swift:

main.swift

import Foundation

var output: CInt = 0
getInput(&output)

println(output)

UserInput.c

#include <stdio.h>

void getInput(int *output) {
    scanf("%i", output);
}

cliinput-Bridging-Header.h

void getInput(int *output);

Here is the original answer.


The compiler converts C API to Swift just like it does for Objective-C.

import Cocoa

let frame = CGRect(x: 10, y: 10, width: 100, height: 100)

import Darwin

for _ in 1..10 {
    println(rand() % 100)
}

See Interacting with Objective-C APIs in the docs.


Just in case you're as new to XCode as me and want to try the snippets posted in Leandro's answer:

  1. File->New->Project
  2. choose Command Line Tool as a project preset and name the project "cliinput"
  3. right-click in the project navigator (the blue panel on the left) and choose "New File..."
  4. In the drop down dialog name the file "UserInput". Uncheck the box "Also create a header file". Once you click "Next" you will be asked if XCode should create the Bridging-Header.h file for you. Choose "Yes".
  5. Copy & paste the code from Leandro's answer above. Once you click on the play button it should compile and run in the terminal, which in xcode is built-in in the bottom panel. If you enter a number in the terminal, a number will be returned.

This post also has a good explanation regarding how to do this using clang's module support.

It's framed in terms of how to do this for the CommonCrypto project, but in general it should work for any other C library you want to use from within Swift.

I briefly experimented with doing this for zlib. I created a new iOS framework project and created a directory zlib, containing a module.modulemap file with the following:

module zlib [system] [extern_c] {
    header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/zlib.h"
    export *
}

Then under Targets -> Link Binary With Libraries I selected add items and added libz.tbd.

You may want to build at this point.

I was then able to write the following code:

import zlib

public class Zlib {
    public class func zlibCompileFlags() -> UInt {
        return zlib.zlibCompileFlags()
    }
}

You don't have to put the zlib library name in front, except in the above case I named the Swift class func the same as the C function, and without the qualification the Swift func ends up being called repeatedly until the application halts.