3

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:

enter image description here enter image description here

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

enter image description here

Why is that? I've got the .mm file and it is supposed to compile as the Objective-C++ source.

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

8 Comments

No. The OpenCV import statement is importing the Objective-C part of OpenCV to the xxx.h. You are wrong. I know what you are saying about but this is not the case. The exactly same setup with opencv 2 works fine and I have a working project with it.
@Grigory Hmmm... i didn't mean to be patronising. Sometimes you have to guess the level of knowledge in the question. In fact just now I was comparing this over-explained answer with one I consider far too terse. You are quite right about that header being objective-C, I hadn't checked. IN any case I have followed your exact setup and cannot see the issue. My version compiles correctly.
What version of Xcode do you have? I checking it with Version 7.0 (7A220)
The same - Version 7.0 (7A220)
Is it possible that you archive your project (with the framework) and put it somewhere on a fileexchange so I can open it on my machine? I'm trying to solve the issue since yesterday and completely tired of it..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.