1

we are trying to use nats library for iOS/Swift Project. We haven’t gotten a direct swift library to integrate with iOS project. Hence, we are trying to use nats.c library inside our project. We are doing this by generating static library out of nats.c project with cmake.

Even though we are able to generate .a file static library file for iOS architecture, but we are blocked on how to import these headers and link this binary into our project. Using build phases to link binary file into xcode project we tried but it’s not giving us options to create a wrapper around it using swift.

Any help through the documentation on integrating or sample project or any other way is really appreciated.

Sivajee Battina
  • 4,124
  • 2
  • 22
  • 45

1 Answers1

1

You need to create a modulemap which points to a static library's headers in order to use them in swift as a module.

I'm writing from my head, so there may be some errors:

example modulemap file:

/* MyCModule.modulemap */
module MyCModule {
    header "MyCModule.h"
    export *
}

info: As far as remember the header path is relative to the modulemap file - so keep it in mind.

next set the Import paths (SWIFT_INCLUDE_PATHS) build setting to point a folder with your modulemap files.

Then you should be able to import the module and use it:

import MyCModule

Of course you need to embed the library into your project as you would normally do (Project Settings > General > Frameworks, Libraries, and Embedded Content > +) and probably need set LIBRARY_SEARCH_PATHS setting as well.

Be carful using C libraries in swift, you might need to manually release memory using appropriate functions in order to avoid memory leaks.

zepar
  • 155
  • 7