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.

Grigory
  • 992
  • 2
  • 18
  • 34

1 Answers1

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.

Community
  • 1
  • 1
foundry
  • 31,615
  • 9
  • 90
  • 125
  • 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 Sep 23 '15 at 11:18
  • @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](http://stackoverflow.com/a/32733984/1375695). 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. – foundry Sep 23 '15 at 11:32
  • What version of Xcode do you have? I checking it with Version 7.0 (7A220) – Grigory Sep 23 '15 at 11:35
  • The same - Version 7.0 (7A220) – foundry Sep 23 '15 at 11:36
  • 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.. – Grigory Sep 23 '15 at 11:43
  • @Grigory - zip file (including opencv framework) http://we.tl/t9Pwv61r9y github (you provide the framework) https://github.com/foundry/swiftopencv – foundry Sep 23 '15 at 11:58
  • Aha! In your project you did not `#import "CVWrapper.h"` in the bridging header. What will you see if you do it? If you don't import the CVWrapper then how will you be able to use it in Swift? In my other project I'm doing exactly the same thing and do not have any problems.. Can you try it, please? – Grigory Sep 23 '15 at 12:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90436/discussion-between-grigory-and-foundry). – Grigory Sep 23 '15 at 12:23