Consume C in Swift Static Library with Bitcode enabled and integrate it in a Swift project

Kishore
3 min readMay 30, 2020

--

Summary

  • Create a Static Library with C and Swift in Xcode
  • Enable Bitcode on the Static Library create
  • Distributing compiled iOS Swift static libraries
  • Integrate the Static Library into a iOS swift application

Create a Static Library with C and Swift in Xcode

Open Xcode and create a new project

Select Static Library and Tap on Next
Enter your project details and Tap on Next

Now add your C code to the project or write the C code. When you create a new C file Xcode will ask to create bridging header file, Tap on Create Bridging Header.

If the bridging header is not create then create a new .h file and add the file path to Objective-C Bridging header in the Target → Build Settings

Linking Bridging Header

Add all the required C header files in the bridging header which needs to be accessed in swift.

Enable Bitcode on the Static Library create

Note: Just setting Enable Bitcodeto YESin the Build Settings will not allow the application to archive

Step 1:

Goto Project Settings → Build Settings → Other C flags and set debug with —fembed-bitcode-marker and release with —fembed-bitcode

Step 2:

Goto Project Settings → Build Settings, click on the + sign at the top to Add User-Defined Setting

Add a new setting by nameBITCODE_GENERATION_MODE and set debug with marker and release with bitcode

Distributing compiled iOS Swift static libraries

Set the active scheme to Generic iOS Device and Tap on build

Once the build is successful, expand Products folder, open the .a file in finder and copy both .a file and .swiftmodule folder and this can used in other project as static library

Note: both .a file and .swiftmodule folder are required

Integrate the Static Library into a iOS swift application

Open an iOS application create a lib folder and add both .a file and .swiftmodule folder into the lib folder and check if the .a file is listed in Frameworks, Libraries, and Embedded Content if not add the file.

Goto Target → Build Settings and check if lib folder is added to Library Search Paths and Import Paths if not add it.

Hurry!!!!!!

Now you are good to go and use the Static Library inside a iOS project.

--

--