简体   繁体   中英

Installing OpenCV 3 on Mac OS X as a framework

I wanted to make an app on OS X El Capitan using OpenCV. I decided to use the latest version, version 3.0.0 released on June 4th, 2015. I had installed version 2.4.x using brew, but since version 3 now has a build script for osx that builds a framework, I wanted to use that method.

After downloading version 3 from the OpenCV.org, I opened a terminal window in the opencv-3.0.0 directory and executed the build_framework.py script as follows: platforms/osx/build_framework.py osx (The osx argument tells the script to make a directory named osx to output the framework there)

Everything built without a hitch so I then added the framework built in the osx directory to my project.

Much to my surprise my project would not build without errors. First of all were the two following problems. Here is a screenshot of the first: opencv.hpp标头问题

If the first problem did not show up, the #ifndef __cplusplus # error for each header file in the opencv2.framework would get triggered.

标头必须编译为C ++

It turns out that problem was that the C++ headers need to be called before the Objective-C headers, so I added the following to a PCH file to the project: 标头BuildOrder

You can actually add this code to the header file that need the opencv.hpp header file instead of make a PCH file for the project. You should also be sure to say #include and not #import, but they should both work.

Once I figured out those problems I was still stuck with 39 undefined symbols for architecture x86_64.

未定义的符号

At first I thought the framework was not including the x86_64 versions, but a quick check revealed that it included both i386 and x86_64 versions of the object files and that my Project settings were all correct. Next I looked up the names of some of the undefined symbols and found that they were part of a project named OpenCL. OpenCL is supposed to accelerate some of the functions and was included in opencv3. At first I thought that the module was not getting built, but after reading through the build_framework.py build script I found that it was getting its settings from the CMakeLists.txt file. Reading this showed that following OpenCL flags: WITH_OPENCL WITH_OPENCLAMDFFT WITH_OPENCLAMDBLAS were being set CMakeLists.txt Orignial

A little more reading lead me to believe that Mac OS X El Capitan is not compatible with OpenCL, as it appears there needs to be kernel support. So I changed the CMakeLists.txt file to not build OpenCL by adding "AND NOT APPLE" WITH_OPENCL WITH_OPENCLAMDFFT WITH_OPENCLAMDBLAS

CMakeLists.txt已更新

After the changes to the CMakeLists.txt file and building the framework again, my program was able to link with opencv2.framework (not sure why they still call it opencv2 instead of opencv3) and run.

It took me more time than I would like to admit, so I thought I would share my experience here in the hopes that it save someone else all the frustration getting OpenCV 3 working with their Mac OS X app. Cheers!

After nearly 2 years, I encountered the same problem. However, I found a more decent solution.

Contrary to your thought, macOS actually is compatible with OpenCL, although it is not linked into the OpenCV2.framework. I tried to link my binary with liblapack.tbd , libcblas.tbd and OpenCL.framework before I link opencv2.framework and it works like a charm.

So just go to the Project Settings -> Build Phases -> Link Binary With Libraries and add the following:

Link Binary With Libraries section

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM