For iOS development, sometimes you want to use a existed native C codes for help. Objective-C is a superset of C so it’s easy to combine C codes in your Objective-C project. But for a library usage, it only support compiling C code to a static library by now.
In this tutorial, I take an open source project message broker as an example to demo how to compile it into a static library in Xcode.
Environments
* Xcode 4.1
* MacOSX Lion
* mosquitto 0.13
Tutorial
1. Download mosquito form <>. Here is the whole directory structure:
2. Create a new project by template C/C++ Library
* Choose static type
3. Import(or just drap and drop) C codes(include .h and .c) to the project
4. Check the “Build settings” in project
* “Base SDK” => Latest iOS
* “Architectures” => Standard(armv6 armv7)
* “Supported Platforms” should include “iphonesimulator iphoneos”
5. Check the “Build Phases” in targets
* “Copy Headers” => Add or move(from “Project”) the header files that should be referenced to “Public”
6. Select Scheme respectively for iOS Device and iPhone/iPad Simulator for Build(Cmd+b) and resolve the errors
* Select iOS Device will make product(.a) in …
* Select iPhone/iPad simulator will make product(.a) in …
* Right-click the product name and click “Show in Finder” to locate files
7. Now you can copy the public header files and static library to the next project you want to use it.
Notes:
1. Only static library(product will be the .a format) support for iOS
2. The compiled library for simulator and physic device is different