Search code examples
swiftstatic-librariesstatic-linking

Using static C library in swift, cannot find module


I need to create a swift wrapper for a C library for use on both iOS and macOS. I have added the .a to the frameworks list and include it in library search path. I have added the header file to project and added it to User header search paths and I have added a module.modulemap to the project as well. Looking like this:

module codinglibc [system][extern_c] {
    header “codinglibc.h”
    export *
}

But when I import the module in Swift:

import Foundation

import codinglibc

I get this error message: No such module 'codinglibc'.

The project is a Cocoa Framework and I have been following this guide: https://medium.com/swift-and-ios-writing/using-a-c-library-inside-a-swift-framework-d041d7b701d9

I have looked at a lot of stackoverflow answers but most have been solved by adding import paths, which I already have done and Xcode can find both the header file and the static library so that is not the issue.

So: 1. Have I done something obviously wrong which I have missed? 2. Should I use briding headers instead?

Edit: I tried enabling Allow Non-modular Includes In Framework Modules still no success


Solution

  • The answer is pretty trivial, yet annoying.

    If you add the module.modulemap in an Xcode project, Xcode will not register it as "to be imported", so what you need to do is to add the path to you module.modulemap file in the header includes.