Bundling C Library in Swift Framework
Article Summary
Lem from Bumble's engineering team tackles a tricky iOS challenge: how do you bundle legacy C libraries into modern Swift frameworks without breaking portability? The common approaches fail when you need to ship via Carthage or CocoaPods.
When Swift launched in 2014, it created new headaches for developers wanting to use C libraries. This deep dive walks through bundling giflib (a C library) into a Swift framework using explicit module maps, a technique that keeps frameworks portable across machines and distribution channels.
Key Takeaways
- Standard module maps break portability: frameworks bind to local file paths
- Explicit modules act like private headers, imported only when needed
- Custom modulemap files override Xcode defaults for clean C library encapsulation
- Result: pure Swift interface with hidden C implementation details
Explicit module maps solve the C library portability problem, letting you ship Swift frameworks with embedded C code that works across Carthage, CocoaPods, and binary distribution.
About This Article
Lem's team at Badoo needed to embed the giflib C library into a Swift framework that would work across different machines and package managers like Carthage and CocoaPods. The challenge was that standard module maps tie frameworks to specific local file paths.
They wrote a custom GifSwift.modulemap file using explicit modules, a Clang feature that lets you declare C libraries as submodules within a parent framework. Then they set Xcode's MODULEMAP_FILE build setting to use this custom map instead of the default one.
The framework could now be distributed and used on different systems. It exposed only a pure Swift interface through the GifFile class, keeping the C implementation completely hidden from anyone using it.