Wednesday, January 20, 2010

Universal C/C++ Static Library for iPhone simulator and device by GCC compiling

This shows how to create universal static library to use c/c++ code for iPhone applications. Here, my cpp code is MathFuncsLib.cpp and MathFuncsLib.h.

I created following script file and run it to get "libMathFuncsLib-fat.a".


#!/bin/bash

/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk -mmacosx-version-min=10.5 -D__IPHONE_OS_VERSION_MIN_REQUIRED=30000 -arch i386 -c -o MathFuncsLib_i386.o MathFuncsLib.cpp
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.1.2.sdk -miphoneos-version-min=3.0 -arch armv6 -c -o MathFuncsLib_armv6.o MathFuncsLib.cpp

/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/ar rc MathFuncsLib_i386.a MathFuncsLib_i386.o
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/ar rc MathFuncsLib_armv6.a MathFuncsLib_armv6.o

/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/ranlib MathFuncsLib_i386.a
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/ranlib MathFuncsLib_armv6.a

lipo MathFuncsLib_i386.a MathFuncsLib_armv6.a -create -output libMathFuncsLib-fat.a

Universal C/C++ Static Library for iPhone simulator and device using Xcode

In my previous post, I googled many articles to create universal static library by using shell script. However, here is a simple and easy way to create universal static library that can be used for both iPhone Simulator and device.

How to create universal C/C++ static library using Xcode
1. Create a new project using Xcode: iPhone OS->Application->OpenGL ES Application using Xcode (e.g., MathFuncsLib)
2. Delete all files from Classes
3. Delete "main.m" from Other Sources
4. Delete the current target "MathFuncsLib"
5. Create new static library target "MathFuncsLib"
6. Add your header (e.g., MathFuncsLib.h) and source (e.g., MathFuncsLib.cpp) files for MathFuncsLib into in Classes
7. Select active SDK: Simulator - your SDK version and Build (library file for x86 architecture)
8. Select active SDK: Device - your SDK version and Build (library file for arm architecture)
9. Find library files for each architecture under a build folder and change their file name appropriately. For example "MathFuncsLib-x86.a" and "MathFuncsLib-arm.a"
10. Copy them into any same folder
11. Create universal static library by running "lipo MathFuncsLib-x86.a MathFuncsLib-arm.a -create -output libMathFuncsLib-fat.a" in Terminal

How to use universal C/C++ static library in your iPhone application
1. Create your iPhone application
2. Open Info for Target, and on Build tab, add your library name for Linking->Other Linker Flags (you should use this format: -l[your library name]. In my case, -lMathFuncsLib-fat)
3. If you create separate folders for library such as "Include" for header files and "Lib" for library file, add them for Search Paths->Header Search Paths and Library Search Paths on the Build tab
4. Import header file for library in your application (e.g., #import "MathFuncsLib/MathFuncsLib.h")
5. Build and Go application
6. If you want to look at your application on iPhone device, you also need to follow the instruction to install your application on device by using "Apple Developer Program"

Thursday, January 14, 2010

How to copy iPhone Application Data to Mac computer

1) Navigate to the app on your phone via the XCode Organizer.

2) Click the triangle on the left of your app's name to show the "Application Data" field.

3) Press the down arrow on the right side of that to download it.

4) Save the application data into your local folder.
--> "Document", "Library", and "tmp" folders will be saved.