Xcode 7 和 openCV(无 Swift):Core.hpp 头文件必须编译为 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35840688/
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
Xcode 7 and openCV (no Swift): Core.hpp header must be compiled as C++
提问by mm24
I have followed the instructionson how to install OpenCV on an iOS project. However when using Xcode 7 I had to add manually a prefix header. Doing this unfortunately did not help and I was still getting compile errors. I then read another post suggesting that is best to add manually the imports and not use prefix headers in Xcode 7, so I did.
我已按照有关如何在 iOS 项目上安装 OpenCV的说明进行操作。但是,在使用 Xcode 7 时,我必须手动添加前缀标头。不幸的是,这样做没有帮助,我仍然遇到编译错误。然后我读了另一篇文章,建议最好手动添加导入,而不是在 Xcode 7 中使用前缀标头,所以我这样做了。
Here is my code:
这是我的代码:
#import "ViewController.h"
#import <opencv2/opencv.hpp>
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <opencv2/highgui/cap_ios.h>
//using namespace cv;
@interface ViewController ()
{
IBOutlet UIImageView* imageView;
IBOutlet UIButton* button;
}
- (IBAction)actionStart:(id)sender;
@end
However I still get the following errors.
但是我仍然收到以下错误。
When I uncomment the using namespace cv; I get the following:
当我取消注释 using 命名空间 cv; 我得到以下信息:
I found some complex solutions talking about exposing headers to Swift etc.. I just want my project to work on Objective-C with Xcode 7 ...
我发现了一些关于向 Swift 公开标头等的复杂解决方案。我只是希望我的项目能够使用 Xcode 7 在 Objective-C 上工作......
回答by Petesh
OpenCV is a C++
framework, which means that any code that makes use of OpenCV has to be compiled with C++
interpretation, rather than C
interpretation.
OpenCV 是一个C++
框架,这意味着任何使用 OpenCV 的代码都必须通过C++
解释来编译,而不是C
解释。
The errors you see, e.g. with the using namespace cv;
indicate that the code is compiled using the objective-C compiler, rather than the objective-C++ compiler.
您看到的错误,例如,using namespace cv;
表明代码是使用目标 C 编译器而不是目标 C++ 编译器编译的。
As I mentioned in my comment the easiest way to get this to happen is to ensure that any file that #include
s an opencv header must be named e.g. ViewController.mm
, i.e. it must be an Objective-C++ file.
正如我在评论中提到的,最简单的方法是确保任何#include
包含 opencv 头文件的文件都必须命名为 eg ViewController.mm
,即它必须是一个 Objective-C++ 文件。
Alternatively, you can select and override the Type
of the file, by explicitly selecting the Objective-C++ Source
option for the file type in the utilities pane.
或者,您可以Type
通过Objective-C++ Source
在实用程序窗格中明确选择文件类型选项来选择和覆盖文件的。
回答by bRo
I just had the exact same problem. I'm working in a Swift project with OpenCV.
我只是遇到了完全相同的问题。我正在使用 OpenCV 参与一个 Swift 项目。
Regarding Swift, its entry point to OpenCV is a file I called OpenCVWrapper. So I got OpenCVWrapper.h and OpenCVWrapper.mm. In the bridging header of my project, I got #import "OpenCVWrapper.h".
关于 Swift,它对 OpenCV 的入口点是一个我称为 OpenCVWrapper 的文件。所以我得到了 OpenCVWrapper.h 和 OpenCVWrapper.mm。在我的项目的桥接头中,我得到了 #import "OpenCVWrapper.h"。
Thing is I wanted to write a class called MatUtils in Objective-C++ that I could call from OpenCVWrapper.mm. For them to be seeable in there, I had to put them in MatUtils.h.
事情是我想在 Objective-C++ 中编写一个名为 MatUtils 的类,我可以从 OpenCVWrapper.mm 调用它。为了让它们在那里可见,我不得不将它们放在 MatUtils.h 中。
Long story short, the mistake is that in OpenCVWrapper.h, I did #import "MatUtils.h". MISTAKE!!!! As OpenCVWrapper is in the bridging header, C++ is now reachable from Swift!
长话短说,错误在于在 OpenCVWrapper.h 中,我做了 #import "MatUtils.h"。错误!!!!由于 OpenCVWrapper 位于桥接头中,因此现在可以从 Swift 访问 C++!
Quick fix : #import "MatUtils.h" in OpenCVWrapper.mm!
快速修复:#import "MatUtils.h" in OpenCVWrapper.mm!
Cheers! bRo
干杯! 兄弟