Cross-compiling for iPhone

In Uncategorized on December 20, 2008 at 3:38 am

Edit November 24, 2010: Building OpenCV

If you are here just to cross-compile OpenCV, please visit this new post instead.  The older post is a little more detailed, error-prone, pedantic, and maybe out of date.

Original post:

Cross-compiling is one of the many joys of iPhone development.  Generally cross compiling is as easy as calling a `configure’ script with the correct parameters:

In our case for putting OpenCV on the iPhone, we used the following command:

# build arm version
echo "Building iPhoneOS arm version of the OpenCV framework"
echo "============================================="
if test -d ../build_iphoneos; then cd ../build_iphoneos; fi
./configure \
--host="arm-apple-darwin9" --target=arm-apple-darwin9 \
--without-python --without-gtk --without-swig --disable-app \
--enable-static \
--disable-shared \
CC=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.0 \
CPP=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/cpp \
CXXCPP=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/cpp \
AR=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/ar \
RANLIB=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/ranlib \
NM=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/nm \
CFLAGS="-arch armv6 -pipe -std=c99 -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -Wreturn-type -Wunused-variable -fmessage-length=0 -fvisibility=hidden -miphoneos-version-min=2.0 -gdwarf-2 -mthumb -miphoneos-version-min=2.0 -I/Library/iPhone/include -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.sdk" \
LDFLAGS="-arch armv6 -pipe -std=c99 -gdwarf-2 -mthumb -I/Library/iPhone/include -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.0.sdk" \
CXXFLAGS="-arch armv6 -pipe -std=c99 -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -Wreturn-type -Wunused-variable -fmessage-length=0 -fvisibility=hidden -miphoneos-version-min=2.0 -gdwarf-2 -mthumb -miphoneos-version-min=2.0 -I/Library/iPhone/include -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.sdk"

the CC, CPP, CXX, CXXCPP, AR, RANLIB, and NM arguments specify the executable files in charge of (CC) compiling C, (CPP) preprocessing C, (CXX & CXXCPP) the same for C++, (AR, RANLIB, NM) library related minutae.  A lot of the CFLAGS LDFLAGS CXXFLAGS are redundant or unnecessary, but they work so why not?

The host= and target= options control cross-compilation.  You really only need to set HOST, which is the machine type on which the compiled code will run.  In our case it is arm-apple-darwin9 which is the machine type of the iPhone.  We gathered a lot of these arguments by building something in XCode and looking at the exact arguments to the C compiler. (You can observe those in the same window as the error messages.)

The C family of programming languages has dumbed down our lives a great deal lately.  Cross-compiling is an even dumber phenomenon than the ins and outs of normal iPhone programming.  Hopefully this post will help bring the joys of cross-compiling into your own living room.  If you find this helpful, please comment on what library you have successfully put on the phone.  If anyone wants OpenCV, let us know.

  1. Thanks for posting the details… It’s too bad that there’s no way to get a live video feed from the iPhone camera however. Thus, most of the CV applications that rely on motion etc are simply not possible (yet?)

  2. Hi, thanks for this little build script!

    I’ve tried to get it working, to no avail. Configure complains about some unknown arguments. Which tarball are you building from?

    Please let me know as soon as you can. I’d love to port a tiny CV app i wrote to the iphone!

  3. I did not include the whole script in the post, so that may explain it. Here’s a link to the full make_iphone.sh:

    It seeems that I am using the ” OpenCV 1.1pre1, October, 2008″ version.

  4. Oh, you should note that the framework-building part of the above script does not work. You will instead have to use the .a files it produces. If you can figure out how to make iPhone frameworks I would love to hear about it.

  5. Do you have a sample xcode project which actually compiles and maybe runs a small demo on the iphone with OpenCV? In particular, I need to be able to find colored square regions in a photo, would OpenCV be able to do this even with the ARM processor and limited RAM etc?

  6. I am also having trouble running the script, is it possible to post the actual files created in a zip?


  7. […] ************************************ 【追記】 Cross-compiling for iPhone « LambdaJiveという興味深いエントリ発見. […]

  8. […] 画像処理/認識ライブラリOpenCVのビルド方法が載ってるそうです。 (Thanks iphone_dev_jp@Twitter) これを使えば顔の認識などが可能ですがiPhoneの処理速度でどこまでいけるのか… Cross-compiling for iPhone https://lambdajive.wordpress.com/2008/12/20/cross-compiling-for-iphone/ […]

  9. Thanks for inputs. I’ve modified your script to fix framework generation errors. Details are here: http://zaaghad.blogspot.com/2009/02/universal-i386arm-opencv-framework-for.html

  10. Thanks a lot — this is helpful in getting a start at throwing this on my iPhone. Now if only Apple would give us full access to the video stream.

    Too bad there isn’t a way to peek at the video buffer and steal the feed from the camera app.

  11. So anyone had applied opencv to iPhone successfully ? I heard a lot to compiler it for arm/iphone, and used it for iPhone simulator, so how about the final ?
    Please share with me : forrest.shi gmail.com

  12. seem like that there is none application which used opencv in Iphone ? Please correct me if now you can use it or you found that.

    that will be great to build one xcode project which use static opencv library, because that is the only way to use 3 rd party library.

    if you have interests to talk about this topic, welcome to email to me : forrest.shi gmail.com

  13. […] Basics of cross-compiling: Cross-compiling for iPhone […]

  14. […] you are here just to cross-compile OpenCV, follow these simple instructions.  The older post is a little more detailed, error-prone, pedantic, and maybe out of […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: