简体   繁体   English

在Mac OS X上安装OpenCV 3作为框架

[英]Installing OpenCV 3 on Mac OS X as a framework

I wanted to make an app on OS X El Capitan using OpenCV. 我想使用OpenCV在OS X El Capitan上制作应用程序。 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. 我决定使用2015年6月4日发布的最新版本3.0.0。我使用brew安装了2.4.x版本,但由于版本3现在有一个构建框架的osx构建脚本,我想使用它方法。

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) 从OpenCV.org下载版本3后,我在opencv-3.0.0目录中打开了一个终端窗口并执行了build_framework.py脚本,如下所示:platforms / osx / build_framework.py osx(osx参数告诉脚本编写一个名为osx的目录,用于输出框架)

Everything built without a hitch so I then added the framework built in the osx directory to my project. 所有内容都顺利构建,因此我将osx目录中构建的框架添加到我的项目中。

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. 如果第一个问题没有显示,opencv2.framework中每个头文件的#ifndef __cplusplus#错误将被触发。

标头必须编译为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: 事实证明,问题是需要在Objective-C头之前调用C ++头,因此我将以下内容添加到项目的PCH文件中: 标头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. 您实际上可以将此代码添加到需要opencv.hpp头文件的头文件中,而不是为项目创建PCH文件。 You should also be sure to say #include and not #import, but they should both work. 您还应该确定说#include而不是#import,但它们都应该有效。

Once I figured out those problems I was still stuck with 39 undefined symbols for architecture x86_64. 一旦我发现了这些问题,我仍然坚持使用39个未定义的符号来构建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. 起初我认为该框架不包括x86_64版本,但快速检查显示它包括i386和x86_64版本的目标文件,并且我的项目设置都是正确的。 Next I looked up the names of some of the undefined symbols and found that they were part of a project named OpenCL. 接下来,我查找了一些未定义符号的名称,发现它们是名为OpenCL的项目的一部分。 OpenCL is supposed to accelerate some of the functions and was included in opencv3. OpenCL应该加速一些功能,并包含在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. 起初我认为该模块没有构建,但在阅读build_framework.py构建脚本后,我发现它是从CMakeLists.txt文件获取其设置。 Reading this showed that following OpenCL flags: WITH_OPENCL WITH_OPENCLAMDFFT WITH_OPENCLAMDBLAS were being set 读取此信息表明,在OpenCL标志之后:正在设置WITH_OPENCL WITH_OPENCLAMDFFT WITH_OPENCLAMDBLAS 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. 稍微阅读让我相信Mac OS X El Capitan与OpenCL不兼容,因为它似乎需要内核支持。 So I changed the CMakeLists.txt file to not build OpenCL by adding "AND NOT APPLE" WITH_OPENCL WITH_OPENCLAMDFFT WITH_OPENCLAMDBLAS 因此我通过添加“AND NOT APPLE”WITH_OPENCL WITH_OPENCLAMDFFT WITH_OPENCLAMDBLAS将CMakeLists.txt文件更改为不构建OpenCL

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. 在更改了CMakeLists.txt文件并再次构建框架之后,我的程序能够与opencv2.framework链接(不知道为什么他们仍称它为opencv2而不是opencv3)并运行。

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. 我花了更多的时间来承认,所以我想我会在这里分享我的经验,希望能够让其他人免于让OpenCV 3使用他们的Mac OS X应用程序而感到沮丧。 Cheers! 干杯!

After nearly 2 years, I encountered the same problem. 经过近2年,我遇到了同样的问题。 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. 与您的想法相反,macOS实际上与OpenCL兼容,尽管它没有链接到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. 在链接OpenCL.framework之前,我尝试将我的二进制文件与liblapack.tbdlibcblas.tbdOpenCL.framework链接opencv2.framework ,它就像魅力一样。

So just go to the Project Settings -> Build Phases -> Link Binary With Libraries and add the following: 所以只需转到项目设置 - >构建阶段 - >使用库链接二进制文件并添加以下内容:

Link Binary With Libraries section Link Binary With Libraries部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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