xcode 混合 Objective-C 和 C++ 和 OpenCV

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6128097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:07:08  来源:igfitidea点击:

Mix Objective-C and C++ and OpenCV

objective-cxcodeiosopencvxcode4

提问by Linus

I'm coding an iPhone app and I'm using OpenCV for some image processing. I have only used it in plain C so far but now I need to use C++ to create some basic OCR.

我正在编写一个 iPhone 应用程序,我正在使用 OpenCV 进行一些图像处理。到目前为止,我只在普通 C 语言中使用过它,但现在我需要使用 C++ 来创建一些基本的 OCR。

I first created a .h/.cpp file and it seems to compile fine. But I need to mix this with some Objective-C to open images and so on. I then renamed the file to .mm instead for .cpp but it won't compile!

我首先创建了一个 .h/.cpp 文件,它似乎编译得很好。但是我需要将它与一些 Objective-C 混合以打开图像等等。然后我将文件重命名为 .mm 而不是 .cpp 但它不会编译!

I get this error: "Statement-expressions are allowed only inside functions" in OpenCV core.hpp line 432

我收到此错误:OpenCV core.hpp 第 432 行中的“仅在函数内部允许使用语句表达式”

Line 432 is this line:

第 432 行是这一行:

typedef Matx<_Tp, MIN(m, n), 1> diag_type;

Any ideas why this might happen?

任何想法为什么会发生这种情况?

回答by mets19

I had the same issue. I imported the Open-CV header before the UIKit headers. Make sure you do this in the pch file. The issue is with some macro defined in both UIKit and OpenCV.

我遇到过同样的问题。我在 UIKit 头文件之前导入了 Open-CV 头文件。确保在 pch 文件中执行此操作。问题在于 UIKit 和 OpenCV 中定义的一些宏。

Source: http://computer-vision-talks.com/2011/01/using-opencv-in-objective-c-code/

来源:http: //computer-vision-talks.com/2011/01/using-opencv-in-objective-c-code/

Maunil

毛尼尔

回答by speby

I actually relied on the private framework (and sample project) provided by this guy: http://aptogo.co.uk/2011/09/opencv-framework-for-ios/

我实际上依赖于这个人提供的私有框架(和示例项目):http: //aptogo.co.uk/2011/09/opencv-framework-for-ios/

I managed to use the same approach he did in the sample for use in my Objective-C mac app. Namely, the use of the import directive in the .PCH file as well as using .mm for Objective-C files that relied on any OpenCV C++ code/classes.

我设法使用他在示例中使用的相同方法在我的 Objective-C mac 应用程序中使用。即,在 .PCH 文件中使用 import 指令以及将 .mm 用于依赖于任何 OpenCV C++ 代码/类的 Objective-C 文件。

回答by Chris

Another (unrelated?) thing worth mentioning, at least in xcode 4.5, is that you have to change your C++ standard library to 'libstdc++' in your target settings to get openCV to compile.

另一件(不相关的?)值得一提的事情,至少在 xcode 4.5 中,您必须在目标设置中将 C++ 标准库更改为“libstdc++”才能编译 openCV。

回答by Dattatray Deokar

the OpenCV headers must be included before UIKit.h and Foundation.h because OpenCV defines a MIN macro that conflicts with the MIN function defined by the Apple frameworks. If you include the OpenCV headers after UIKit.h and Foundation.h you will receive compilation errors such as ‘LLVM GCC 4.2 Error: Statement-expressions are allowed only inside functions'. Including the OpenCV headers first and surrounding the #import with the __cplusplus conditional test avoids this problem and means that you can still use plain Objective-C for ‘.m‘ files in your project that don't call the OpenCV APIs.

OpenCV 头文件必须包含在 UIKit.h 和 Foundation.h 之前,因为 OpenCV 定义了一个与 Apple 框架定义的 MIN 函数冲突的 MIN 宏。如果在 UIKit.h 和 Foundation.h 之后包含 OpenCV 头文件,您将收到诸如“LLVM GCC 4.2 错误:语句表达式仅在函数内部允许”之类的编译错误。首先包含 OpenCV 标头并使用 __cplusplus 条件测试围绕 #import 避免了这个问题,这意味着您仍然可以将普通的 Objective-C 用于项目中不调用 OpenCV API 的“.m”文件。

http://aptogo.co.uk/2011/09/opencv-framework-for-ios/

http://aptogo.co.uk/2011/09/opencv-framework-for-ios/