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

1 comment:

dakopyc said...

FYI looks like (per the manpage for "libtool"), that the update to this is now:

"Libtool with -static is intended to replace ar(5) and ranlib."

so in place of "ar" followed by "ranlib", you can now simply invoke:

libtool -o $(OUT_DIR)/libMyLibrary.a -static $(OBJS)

hth! ;) cheers, thx,

-dk