Thursday, 3 July 2008

Creating an universal static OpenCV library with iPhone support

This post has been updated here. Sorry I have no time to upgrade the iPhone part, I hope someone will share this knowledge ;)

We will create a minimal static universal OpenCV 1.0.0 library with 4 architectures: Intel, PowerPC, x86_64 and Arm for the iPhone. I've been testing it on Leopard and Snow Leopard but it should work on any Mac OS X version.

First checkout or untar the OpenCV sources in the opencv-1.0.0 directory. Then, on the same level (not inside) create a build directory with 4 subdirectories: ppc, i386, x86_64 and arm. (Download the code from Sourceforge)

Then set the global configure settings:
$ export CONFFLAGS="--without-imageio --without-python --without-swig --disable-apps --disable-dependency-tracking --without-carbon --without-quicktime --enable-shared=no --without-gtk"
in the ppc dir:
$ ../../opencv-1.0.0/configure ${CONFFLAGS} --target=ppc-apple-darwin9
$ make CXXFLAGS="-arch ppc"
in the i386 dir:
$ ../../opencv-1.0.0/configure ${CONFFLAGS} --target=i386-apple-darwin9
$ make CXXFLAGS="-arch i386"
in the x86_64 dir:
$ ../../opencv-1.0.0/configure ${CONFFLAGS} --target=x86_64-apple-darwin9
$ make CXXFLAGS="-arch x86_64"

in the arm dir:
$ ../../opencv-1.0.0/configure ${CONFFLAGS} --host =arm-apple-darwin9 CXX=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin9-g++-4.2.1 CXXFLAGS="-arch armv6 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk" CXXCPP=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/cpp
$ make
the last one is tough, so let's see the variables one by one:
CXX=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin9-g++-4.2.1
CXXFLAGS="-arch armv6 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk"
CXXCPP=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/cpp
Ok. Now for the universal static libs:
$ lipo -create i386/cv/src/.libs/libcv.a x86_64/cv/src/.libs/libcv.a ppc/cv/src/.libs/libcv.a armv6/cv/src/.libs/libcv.a -output libcv.a
$ lipo -create i386/cxcore/src/.libs/libcxcore.a x86_64/cxcore/src/.libs/libcxcore.a ppc/cxcore/src/.libs/libcxcore.a armv6/cxcore/src/.libs/libcxcore.a -output libcxcore.a
$ lipo -create i386/cvaux/src/.libs/libcvaux.a x86_64/cvaux/src/.libs/libcvaux.a ppc/cvaux/src/.libs/libcvaux.a armv6/cvaux/src/.libs/libcvaux.a -output libcvaux.a
$ lipo -create i386/ml/src/.libs/libml.a x86_64/ml/src/.libs/libml.a ppc/ml/src/.libs/libml.a armv6/ml/src/.libs/libml.a -output libml.a
$ lipo -create i386/otherlibs/highgui/.libs/libhighgui.a x86_64/otherlibs/highgui/.libs/libhighgui.a ppc/otherlibs/highgui/.libs/libhighgui.a armv6/otherlibs/highgui/.libs/libhighgui.a -output libhighgui.a

That's all. Oh, try
$ make -j 2

:)