Badoo lem Dec 12, 2018

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

Critical Insight

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.

The article includes a complete working example with giflib and reveals why the 'most common' approach actually fails for external distribution.

About This Article

Problem

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.

Solution

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.

Impact

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.