19

When I tried to compile my XCode project with OpenCV 2.4 iOS using XCode 7 + iOS SDK 9, XCode complained that

ld: 'opencv2.framework/opencv2(alloc.o)' does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture arm64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

and refused to link. After some googling, it turns out to be because Apple added a new feature named Bitcode for app optimization within App Store. While OpenCV iOS binary hasn't been updated to include Bitcode, it cannot pass the link stage.

Some reference pointed out a temporary solution to disable ENABLE_BITCODE so the linking could be done without Bitcode. This will prevent the app being compiled for Apple Watches because Bitcode is mandatory for Watch Apps. Therefore my question is, are there some (best easy) ways to compile iOS OpenCV with Bitcode enabled? (better with a download link for compiled framework)

Community
  • 1
  • 1
grapeot
  • 1,594
  • 10
  • 21

2 Answers2

22

After some search and trial, I figured out a way to compile OpenCV iOS from the source with Bitcode. A compiled binary is also provided here: [v3.0] [v2.4]. [Disclaimer: I am not responsible for the integrity of the compiled binary. Use at your own risk.]

The steps of compilation is basically the same as the official document, with only one extra step.

  1. Download the code with git:

    cd ~/<my_working_directory>

    git clone https://github.com/Itseez/opencv.git

  2. Make symbolic link for Xcode to let OpenCV build scripts find the compiler, header files etc.

    cd /

    sudo ln -s /Applications/Xcode.app/Contents/Developer Developer

  3. [Key Step] Change the compilation script to add the extra option for Bitcode: edit ~/<my_working_directory>/opencv/platform/ios/build_framework.py, and locate the line containing -DCMAKE_C_FLAGS. Add a flag of -fembed-bitcode. For example, in the source I got, it's line 55, and will look like

    "-DCMAKE_C_FLAGS=\"-Wno-implicit-function-declaration -fembed-bitcode\" " +

    after the change. [ref]

  4. Build OpenCV framework:

    cd ~/<my_working_directory>

    python opencv/platforms/ios/build_framework.py ios

    If everything’s fine, a few minutes later you will get ~/<my_working_directory>/ios/opencv2.framework. You can add this framework to your Xcode projects.

P.S. Ask a question, even when you already know the answer is encouraged according to this post on Meta Stackchange.

Community
  • 1
  • 1
grapeot
  • 1,594
  • 10
  • 21
  • 2
    I found that the new version of opencv (version 3.1) already has the -fembed-bitcode flag in the build_framwork.py file – CSawy Aug 09 '16 at 00:58
  • @CSstudent Thanks for the heads up! – grapeot Aug 09 '16 at 16:08
  • @grapeot I have followed the same steps but facing the issues as step 4. Kindly help me. I can show you the logs but they are exceeding the character limit. So cant post here. – Himanshu Garg Jan 04 '17 at 13:32
  • @grapeot Please refert to following link. I have attached a screenshot here. http://stackoverflow.com/q/41465600/3572586 – Himanshu Garg Jan 04 '17 at 13:59
  • It doesn't seem to work with python 3, I had to do `python2.7 opencv/platforms/ios/build_framework.py ios` – Guig Apr 18 '17 at 21:10
8

OpenCV is precisely the kind of software (along with audio and video Codecs) that is likely to have hand-rolled ARM NEON optimisations. The documentation suggests that ~40 functions have had this treatment in OpenCV3.0.

If compiling for LLVM bit-code you'll get the generic (less optimised, implemented in C or C++) versions instead.

Use of Bitcode is optional - except when compiling for Apple watch, where it's hard to imagine you'd run computationally complex image processing anyway. If you're bundling a watch app, override the build setting for bitcode on it only.

marko
  • 9,029
  • 4
  • 30
  • 46