Xcode 7不使用C ++框架构建项目 [英] Xcode 7 does not build project with c++ framework

查看:930
本文介绍了Xcode 7不使用C ++框架构建项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用新版本的opencv 3.0.0框架构建项目(版本2没有这个问题)。
Xcode 7不会像c ++一样编译c ++源码。



这是最简单的设置,不是建立:


  1. 从这里下载3.0.0框架


    1. Build。

    这里是编译器说的:

      opencv2.framework / Headers / core / base。 hpp:49:4:错误:base.hpp头必须编译为C ++ 



    为什么?我有 .mm 文件,它应该编译为Objective-C ++源码。

    解决方案

    openCV import语句在你的Objective-C头文件中,所以暴露给Swift。它应该在实现文件中,xxx.mm,这是从Swift隐藏的。如果你移动它,你会发现你的项目编译。



    Swift可以桥接到Objective-C,但如果暴露C ++代码,它将失败。你的Objective-C类应该被看作是你的Swift项目和openCV的C ++接口之间的包装接口。 Objective-C可以与两者协同工作。从Swift中调用Objective-C方法,这些方法可以在xxx.mm实现中使用openCV函数。



    这不是openCV2 vs openCV3的问题。



    更新 p>

    Grigory指出,特定的OpenCV头是纯粹的Objective-C - 它是openCV的iOS视频接口,所以它应该工作。我的设置不正确,因为我的桥接头是空的。添加 #importCVWrapper.h确实打破了构建。



    问题是由openCV头。 cap_ios.h 不是纯Objective-C:它导入一个C ++文件:

      #includeopencv2 / core.hpp

    该文件被设置为抛出错误if C ++编译不可用

      #ifndef __cplusplus 
    #error core.hpp标题必须编译为C ++
    #endif

    由于你没有控制框架,我反正采取隐藏所有标题背后我自己的包装:不能保证任何openCV头文件将被屏蔽从C ++。我想你会想要一个更复杂的包装器访问openCV函数从Swift端。



    这是我 详细说明 其他地方,其中使用openCV和Swift的小型示例项目



    更新



    与Grigory讨论后,他同意:


    我已经做了一个包装器,它工作..似乎而不是找出为什么用opencv2工作,最好使用包装器。


    包装规则!在某些情况下,看起来似乎有点过度,但通常会帮你省下麻烦。这在混合Objective-C和C ++时已经是很好的建议,但在与Swift交互时就成为必要的。


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

    Here's the simplest setup that is not building:

    1. Download the 3.0.0 framework from here http://netix.dl.sourceforge.net/project/opencvlibrary/opencv-ios/3.0.0/opencv2.framework.zip
    2. Create the simplest ios project with Swift language.
    3. Drag-and-drop the framework to the project.
    4. Create the Objective-C++ source and headers and add them to the project.
    5. Create the bridging header.

    Here's the setup:

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

    解决方案

    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.

    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.

    This is not an issue of openCV2 vs 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.

    The issue is caused by that openCV header. cap_ios.h is not pure Objective-C: it imports a C++ file:

        #include "opencv2/core.hpp"
    

    that file is setup to throw an error if C++ compilation is not available

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

    This is the approach I have elaborated elsewhere, with a small sample project using openCV and 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.

    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.

    这篇关于Xcode 7不使用C ++框架构建项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆