I have added a C/C++ dylib library, with C Public API headers to Swift Project.
Assume that the library is, libModule.dylib.
Create a Directory with "Module"
mkdir Module; cd Module
Create a file module.map inside Module directory
Touch module.map
create a directory, this will be the header files directory, any name will serve.
mkdir Headers
copy all headers into Headers directory
cp -R path/Headers Headers/
open module.map & copy below contents to it, Swift team suggest, adding prefix C in front of library name, in case if its a C library.
module CModule {
umbrella "Headers" // for multiple files
header "filename.h" // for single header file, in this case even the Header directory is not needed.
export *
}
Go to Build Settings -> Swift Compiler - Search Paths
- Add complete Module directory path to "Import Paths" key.
- Go to Build Phases, Add New Copy Files Phase.
- Drag libModule.dylib to the Copy Files phase. Select Destination to "Frameworks".
- If required add the libModule.dylib to Link Binary With Libraries section of Build Phases.
If step 9 is missed, we get famous @rpath/ Image not found crash.
Now Import module in swift as below:
import CModule // name given inside module.map
For system library, most steps don't require.. just create directory & module map file with correct header path and do 6, 7.
Its seems step 10 not even required as its a system library.
module CommonCrypto [system] {
header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/CommonCrypto/CommonCrypto.h"
export *
}