简体   繁体   中英

Xcode 7 does not build project with c++ framework

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM