Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile OpenCV iOS with ENABLE_BITCODE

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)

like image 742
grapeot Avatar asked Sep 22 '15 07:09

grapeot


1 Answers

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.

like image 174
grapeot Avatar answered Oct 18 '22 01:10

grapeot