Xcode 7 不使用 C++ 框架构建项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32735545/
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 does not build project with c++ framework
提问by Grigory
I can't build project with a new version of the opencv 3.0.0 framework (version 2 did not have this problem). Xcode 7 does not compile c++ sources as c++.
我无法使用新版本的 opencv 3.0.0 框架构建项目(版本 2 没有这个问题)。Xcode 7 不会将 c++ 源代码编译为 c++。
Here's the simplest setup that is not building:
这是不构建的最简单的设置:
- Download the 3.0.0 framework from here http://netix.dl.sourceforge.net/project/opencvlibrary/opencv-ios/3.0.0/opencv2.framework.zip
- Create the simplest ios project with Swift language.
- Drag-and-drop the framework to the project.
- Create the
Objective-C++
source and headers and add them to the project. - Create the bridging header.
- 从这里下载 3.0.0 框架http://netix.dl.sourceforge.net/project/opencvlibrary/opencv-ios/3.0.0/opencv2.framework.zip
- 用 Swift 语言创建最简单的 ios 项目。
- 将框架拖放到项目中。
- 创建
Objective-C++
源和标题并将它们添加到项目中。 - 创建桥接头。
Here's the setup:
这是设置:
- Build.
- 建造。
And here's what the compiler says:
这是编译器所说的:
opencv2.framework/Headers/core/base.hpp:49:4: error: base.hpp header must be compiled as C++
Why is that? I've got the .mm
file and it is supposed to compile as the Objective-C++ source.
这是为什么?我有这个.mm
文件,它应该编译为 Objective-C++ 源代码。
回答by foundry
The openCV import statement is in your Objective-C header file, so is exposed to Swift. It should be in the implementation file, xxx.mm, which is hidden from Swift. If you move it you will find that your project compiles.
openCV 导入语句位于您的 Objective-C 头文件中,因此暴露给 Swift。它应该在对 Swift 隐藏的实现文件 xxx.mm 中。如果您移动它,您会发现您的项目已编译。
Swift can bridge to Objective-C, but will fail if you expose C++ code to it. Your Objective-C class should be considered a wrapper interface between your Swift project and openCV's C++ interface. Objective-C can work with both. From Swift you call Objective-C methods, and those methods in turn can use openCV functions in the xxx.mm implementation.
Swift 可以桥接到Objective-C,但是如果你向它公开C++ 代码就会失败。您的 Objective-C 类应该被视为您的 Swift 项目和 openCV 的 C++ 接口之间的包装接口。Objective-C 可以同时使用两者。从 Swift 调用 Objective-C 方法,然后这些方法可以在 xxx.mm 实现中使用 openCV 函数。
This is not an issue of openCV2 vs openCV3.
这不是 openCV2 与 openCV3 的问题。
update
更新
Grigory points out that the particular OpenCV header is pure Objective-C - it is the iOS video interface to openCV, so it should work. And my setup was incorrect as my bridging header was empty. Adding #import "CVWrapper.h"
does indeed break the build.
Grigory 指出特定的 OpenCV 标头是纯 Objective-C - 它是 openCV 的 iOS 视频接口,所以它应该可以工作。我的设置不正确,因为我的桥接头是空的。添加 #import "CVWrapper.h"
确实会破坏构建。
The issue is caused by that openCV header. cap_ios.h
is not pure Objective-C: it imports a C++ file:
该问题是由 openCV 标头引起的。cap_ios.h
不是纯 Objective-C:它导入一个 C++ 文件:
#include "opencv2/core.hpp"
that file is setup to throw an error if C++ compilation is not available
如果 C++ 编译不可用,该文件被设置为抛出错误
#ifndef __cplusplus
# error core.hpp header must be compiled as C++
#endif
As you have no control over the framework, I would anyway take the approach of hiding all headers behind my own wrapper: there is no guarantee that any openCV header file will be shielded from C++. I'd assume you are going to want a more elaborate wrapper anyway to access openCV functions from the Swift side.
由于您无法控制框架,因此我无论如何都会采取将所有头文件隐藏在我自己的包装器后面的方法:不能保证任何 openCV 头文件都会被 C++ 屏蔽。我假设您无论如何都会想要一个更复杂的包装器来从 Swift 端访问 openCV 函数。
This is the approach I haveelaboratedelsewhere, with a small sample project using openCV and Swift.
这是我在别处阐述的方法,有一个使用 openCV 和 Swift的小示例项目。
update
更新
After some discussion with Grigory, he concurs:
在与格里戈里讨论之后,他同意:
I've made a wrapper and it works.. Seems like instead of finding out why did build with opencv2 work it is better to use a wrapper.
我已经制作了一个包装器并且它可以工作.. 似乎与其找出为什么使用 opencv2 构建可以工作,不如使用包装器。
Wrappers rule! It may seem like overkill in some cases, but it's usually going to save you trouble down the line. This is already good advice when mixing Objective-C and C++, but becomes a necessity when interfacing with Swift.
包装规则!在某些情况下,这似乎有点矫枉过正,但通常可以为您省去麻烦。这在混合 Objective-C 和 C++ 时已经是一个很好的建议,但在与 Swift 接口时就变得必要了。