混合 Objective-C 和 C++和 OpenCV
我正在编写一个 iPhone 应用程序,并使用 OpenCV 进行一些图像处理。到目前为止我只在纯 C 中使用过它,但现在我需要使用 C++ 创建一些基本的 OCR。
我首先创建了一个 .h/.cpp 文件,它似乎编译得很好。但我需要将其与一些 Objective-C 混合来打开图像等。然后我将文件重命名为 .mm 而不是 .cpp,但它无法编译!
我收到此错误: OpenCV core.hpp 第 432 行中的“语句表达式仅允许在函数内部使用”
第 432 行是这一行:
typedef Matx<_Tp, MIN(m, n), 1> diag_type;
任何想法为什么会发生这种情况?
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.
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!
I get this error:
"Statement-expressions are allowed only inside functions" in OpenCV core.hpp line 432
Line 432 is this line:
typedef Matx<_Tp, MIN(m, n), 1> diag_type;
Any ideas why this might happen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我有同样的问题。我在 UIKit 标头之前导入了 Open-CV 标头。确保在 pch 文件中执行此操作。问题在于 UIKit 和 OpenCV 中定义的一些宏。
资料来源: http://computer-vision-
talk.com/2011/01/using-opencv-in-objective-c-code/
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.
Source: http://computer-vision-talks.com/2011/01/using-opencv-in-objective-c-code/
Maunil
我实际上依赖于这个人提供的私有框架(和示例项目): http://aptogo.co.uk/2011/09/opencv-framework-for-ios/
我设法使用他在示例中使用的相同方法在我的 Objective-C mac 应用程序中使用。即,在 .PCH 文件中使用 import 指令,以及对依赖于任何 OpenCV C++ 代码/类的 Objective-C 文件使用 .mm。
I actually relied on the private framework (and sample project) provided by this guy: 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.
另一件(不相关?)值得一提的事情是,至少在 xcode 4.5 中,您必须在目标设置中将 C++ 标准库更改为“libstdc++”才能编译 openCV。
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.
OpenCV 标头必须包含在 UIKit.h 和 Foundation.h 之前,因为 OpenCV 定义的 MIN 宏与 Apple 框架定义的 MIN 函数冲突。如果您在 UIKit.h 和 Foundation.h 之后包含 OpenCV 标头,您将收到编译错误,例如“LLVM GCC 4.2 错误:语句表达式仅允许在函数内部使用”。首先包含 OpenCV 标头并使用 __cplusplus 条件测试围绕 #import 可以避免此问题,并且意味着您仍然可以在项目中对不调用 OpenCV API 的“.m”文件使用普通 Objective-C。
http://aptogo.co.uk/2011/09/opencv- ios 框架/
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.
http://aptogo.co.uk/2011/09/opencv-framework-for-ios/