HowTo compile native C codes to a library for iOS development in Xcode – take Mosquitto as an exampl

HowTo compile native C codes to a library for iOS development in Xcode – take Mosquitto as an example

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 MQTT message broker Mosquitto 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 <http://mosquitto.org/files/source/mosquitto-0.13.tar.gz>. Here is the whole directory structure:

wpid-wpid-Voila_Capture58-2011-10-30-10-483-2011-10-30-10-483.png

2. Create a new project by template C/C++ Library
* Choose static type

wpid-wpid-Voila_Capture60-2011-10-30-10-483-2011-10-30-10-483.png

wpid-wpid-Voila_Capture61-2011-10-30-10-483-2011-10-30-10-483.png

3. Import(or just drap and drop) C codes(include .h and .c) to the project

wpid-wpid-Voila_Capture63-2011-10-30-10-483-2011-10-30-10-483.png wpid-wpid-Voila_Capture64-2011-10-30-10-483-2011-10-30-10-483.png

4. Check the “Build settings” in project

* “Base SDK” => Latest iOS
* “Architectures” => Standard(armv6 armv7)
* “Supported Platforms” should include “iphonesimulator iphoneos”

wpid-wpid-Voila_Capture72-2011-10-30-10-483-2011-10-30-10-483.png

5. Check the “Build Phases” in targets
* “Copy Headers” => Add or move(from “Project”) the header files that should be referenced to “Public”

wpid-wpid-Voila_Capture65-2011-10-30-10-483-2011-10-30-10-483.png

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 …<Debug/Release>-iphoneos
* Select iPhone/iPad simulator will make product(.a) in …<Debug/Release>-iphonesimulator

wpid-wpid-Voila_Capture66-2011-10-30-10-483-2011-10-30-10-483.png wpid-wpid-Voila_Capture76-2011-10-30-10-483-2011-10-30-10-483.png

* Right-click the product name and click “Show in Finder” to locate files
wpid-wpid-Voila_Capture71-2011-10-30-10-483-2011-10-30-10-483.png wpid-wpid-Voila_Capture89-2011-10-30-10-483-2011-10-30-10-483.png

7. Now you can copy the public header files and static library to the next project you want to use it.
wpid-wpid-Voila_Capture79-2011-10-30-10-483-2011-10-30-10-483.png

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

Posted in  Programming. Tags:  . Bookmark the  permalink.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值