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
Tagged , , | Leave a comment

“Localization native development region” setting in Info.plist

In developing iOS project, there’s a setting “Localization native development region” in Info.plist. What does its value affect on the project?

“Localization native development region”  is actually  the key of  “kCFBundleDevelopmentRegionKey” in Info.plist. From the document of “CFBundle Reference”, we get

kCFBundleDevelopmentRegionKey

The name of the development language of the bundle.

When CFBundle looks for resources, the fallback is to look in the lproj whose name is given by the kCFBundleDevelopmentRegionKey in the Info.plist file. You must, therefore, ensure that a bundle contains an lproj with that exact name containing a copy of every localized resource, otherwise CFBundle cannot guarantee the fallback mechanism will work.

Available in iOS 2.0 and later.

Declared in CFBundle.h.

That means its your fallback localization of resources if you don’t provide correspondent language version for user’s selection.

If you just develop only  one language version project and don’t want to do more localization stuff, you can set this to your language region. It will affect the system  localized button like “UIBarButtonSystemItemEdit”

See the following example, we have the value English:

If we change the value to Taiwan

Posted in Programming
Tagged | Leave a comment

Encrypt Perl Module Codes

Normally, the Perl module(.pm) is called in plain text to run. When you need the module codes encrypted, you can use  Module::Crypt to handle. With Module::Crypt, you can encrypt codes and compile them to C library format. Then use XSLoader to load the codes.

EXAMPLE

Lets say you have a module  /myPerl/MOD/myDBI.pm. There’s some DB connection information, like username, password, you’d like to hide it. Originally, you call the module in the following way

use lib qw(/myPerl/MOD);
use myDBI;

Lets encrypt the mode on following steps

1. Install Module::Crypt firstly.

2. Write a script to do the making job. Lets call it make-module_crypt.pl

#!/usr/bin/perl
use Module::Crypt;
CryptModule (
file => 'myDBI.pm',
install_base => '/myPerl/LIB'
);

3. Run make-module_crypt.pl. If it runs without error messages, you will have a new directory auto/ and file myDBI.pm under /myPerl/LIB/. Here myDBI.pm is not the original one. It’s a wrapped module using XSLoader method generated automatically by Module::Crypt. It’s a handy elper to load auto/myDBI/myDBI.so.

4. After that, you can just slightly change the MOD to LIB to call myDBI.

use lib qw(/myPerl/LIB);
use myDBI;

NOTE

  1. It’s “normal” to get the  “Segmentation Fault” message while making. It’s a random error. Just try a couple times to get it done.
Posted in Programming
Tagged | Leave a comment

Remove submodules from Git

要新增 submodules 在自己的 git repository 可以使用 git submodule add 的指令來新增;但要移除時,目前卻沒有相對應的 git submodule rm 指令來操作。要完整地移除 submodules,需要以下幾個步驟:

  1. 刪除 .gitmodules 中相對應的行數
  2. 刪除 .git/config 中相對應的行數
  3. 執行 git rm –cached /path/to/submodule (最後不需要 /)

REFERENCE

* GitSubmodule Tutorial

Posted in Programming
Tagged | Leave a comment