Chinaunix首页 | 论坛 | 博客
  • 博客访问: 463751
  • 博文数量: 145
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1060
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-22 11:52
个人简介

专注计算机技术: Linux Android 云计算 虚拟化 网络

文章分类

全部博文(145)

文章存档

2016年(3)

2015年(21)

2014年(75)

2013年(46)

我的朋友

分类: iOS平台

2014-10-11 03:24:20

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 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:

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

阅读(1635) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~