The Question Mark - blog by Mark Volkmann

Calling C

Overview

Swift code can all functions written in C. This page describes the steps to create a SwiftUI app that includes C code and calls its functions.

Steps

  1. Create a new Xcode project where the interface is “SwiftUI” and the language is “Swift”.

  2. Select File … New … File (or press cmd-n).

  3. In the “Source” category, select “C File”.

  4. Enter a name for the file without a file extension.

  5. Check the “Also create a header file” checkbox.

  6. Click the “Next” button.

  7. Click the “Create” button.

  8. In the dialog that appears, click the “Create Bridging Header” button.

  9. In the generated header file, enter the signatures of the functions you will define. For example:

    long factorial(int n);
    
  10. In the generated C file, enter the function definitions. For example:

    long factorial(int n) {
      if (n == 0 || n == 1) return 1;
      return n * factorial(n-1);
    }
    
  11. In the generated SwiftUICallsC-Bridging-Header.h file, add import directives for each of your C header files. For example:

    #import "factorial.h"
    
  12. Call the C functions in Swift source files. For example, ContentView.swift could contain the following:

    Text("Factorial is \(factorial(5)).") // 120
    

Libraries

To use a C library from Swift:

  1. Build a version of the C library that is compatible with iOS.
  2. Create a group within the Xcode project named “include”.
  3. Copy the .h files for each library into this directory.
  4. Create a group within the Xcode project named “lib”.
  5. Copy the .a files for each library into this directory.
  6. Select the top entry in the project navigator.
  7. Select the project.
  8. Select the “Build Settings” tab.
  9. Open the “Search” section.
  10. Open the “Header Search Paths” entry.
  11. In the “Release” section, add the string “include”.
  12. Open the “Library Search Paths” entry.
  13. In the “Release” section, add the string “lib”.

Invoking Lua

For details on the Lua programming language, see Lua. This contains details on starting the Lua virtual machine from C and deciding which standard libraries should be loaded. For example, by not loading the “os” library, Lua code is unable to read or write files.

  1. Build a version of Lua that is compatible with iOS. See lua-iosx. Enter the following commands to generate the directory frameworks/lua.xcframework:
    • git clone -b 5.4.4 https://github.com/apotocki/lua-iosx
    • cd lua-iosx
    • scripts/build.sh
  2. Create a “Frameworks” group.
  3. Drag the lua-ios/frameworks/lua.xcframework directory into the group.
  4. In the dialog that appears, check the “Copy items if needed” checkbox and click the “Finish” button.

It is now possible to call functions from the Lua C API from functions defined in C source files. Those C functions can be called from Swift code.

For an example of embedding the Lua interpreter in a SwiftUI application, see Swift Calling C and the GitHub respository SwiftUICallsC.