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

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

Xcode 7 and openCV (no Swift): Core.hpp header must be compiled as C++

c++iosobjective-cxcodeopencv

提问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.

但是我仍然收到以下错误。

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

When I uncomment the using namespace cv; I get the following:

当我取消注释 using 命名空间 cv; 我得到以下信息:

enter image description here

在此处输入图片说明

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 Cinterpretation.

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 #includes 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 Typeof the file, by explicitly selecting the Objective-C++ Sourceoption for the file type in the utilities pane. utilities pane drop-down selection

或者,您可以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

干杯! 兄弟

回答by Lau Kwok Ping

Try placing #import < opencv2/opencv.hpp >before #import "ViewController.h".Source

尝试放置#import < opencv2/opencv.hpp >之前#import "ViewController.h"来源