Xcode 8 (Swift): core.hpp header must be compiled as C++ - ios

I am using the OpenCV iOS Framework in a project. I followed the necessary steps to include the framework into the project.
The Project is written using Swift 3.
One of my classes that contains the core functionality of the App is written in Objective-C++. I included the header of the class in my Bridge-header file but when trying to run the project I get the following error:
error core.hpp header must be compiled as C++
After researching online and on SO, the most common solution presented was to create a Wrapper class that would be imported in the bridge header. However, after following this article I face the same problem.
The Header file of my class looks like this:
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#define IMAGE_CLASS UIImage
#elif TARGET_OS_MAC
#import <Cocoa/Cocoa.h>
#define IMAGE_CLASS NSImage
#endif
#import <AGGeometryKit/AGKQuad.h>
#import <stdio.h>
#import <opencv2/opencv.hpp>
#import <Foundation/Foundation.h>
typedef void (^DebugBlock)(cv::Mat current_image, std::string current_image_name);
#interface ImageScanner : NSObject
/**
* Singleton for access to the scanner.
*
* #return Shared scanner.
*/
+ (instancetype)sharedScanner;
Does anyone have an idea what I might be missing?
Thank you in advance!
G.

had the same problem.. solved by importing any file that use openCV in the wrapperClass.mm file ...
SO Answer here

Related

Swift framework - Use Swift class reference in Objective-C class

I am creating Swift framework in which I have to use Objective-C class. So I went through this link. This is the public header of my framework :
#import <UIKit/UIKit.h>
//! Project version number for Test.
FOUNDATION_EXPORT double TestVersionNumber;
//! Project version string for Test.
FOUNDATION_EXPORT const unsigned char TestVersionString[];
// In this header, you should import all the public headers of your framework using statements like #import <Test/PublicHeader.h>
#import <arpa/inet.h>
#import <ifaddrs.h>
#import <netdb.h>
#import <sys/socket.h>
#import <MyTest/MPAppDelegateProxy.h>
Now in class MPAppDelegateProxy, I have to use a Swift class which I have created. This is :
#import "MPAppDelegateProxy.h"
#import "MyTest.h"
#implementation MPAppDelegateProxy
+ (void)proxyAppDelegate {
[MPGlobal MPLog:#"App delegate not set, unable to perform automatic setup." file:#"MPAppDelegateProxy.m" function:#"proxyAppDelegate" line:32];
// rest of code
}
MPGlobal is one of my Swift class. But I am getting :
Use of undeclared identifier 'MPGlobal'
Note : I have added #objC before MPGlobal.
You need to import <Target>-Swift.h file.
This is known as Objective-C Generated Interface Header Name.
You can find it in your Target's build settings.
This file is auto generated by compiler and it needs to be imported in Objective-C files.
change the SWIFT_OBJC_INTERFACE_HEADER_NAME build setting and making it the same across different targets. To do so change the instruction that generates this property from $(SWIFT_MODULE_NAME)-Swift.h to $(PROJECT_NAME)-Swift.h as explained here
After doing this Clean Build Folder by pressing Alt and going into Product menu. Since name of header is shared among targets now it can be imported once in the .m ObjectiveC file and all targets can benefit from Swift classes.
If after building it still shows the error, ensure that the header can be reached from XCode by Cmd clicking on its name. It should open a file that contains code similar to this:
SWIFT_CLASS("_TtC27ProjectName_Summary11MyClass")
#interface MyClass : NSObject
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
#end
If need to ensure that those headers are being generated open a terminal and use this command
find ~/Library/Developer/Xcode/DerivedData -name "*Swift.h"
You should see one header for each target
Another issue that happened to me after those changes is that it started giving errors on ObjectiveC code that I didn't touch. The problem was due to the position of the import, as reported here:
Exactly where at the top of a .m file you #import the hidden bridging
header can make a difference. The usual sign of trouble is that you
get an “Unknown type name” compile error, where the unknown type is a
class declared in Objective-C. The solution is to #import the .h file
containing the declaration for the unknown type in your Objective-C
files as well, before you #import the hidden bridging header. Having
to do this can be an annoyance, especially if the Objective-C file in
question has no need to know about this class, but it resolves the
issue and allows compilation to proceed.
At the very end the code compiles and runs on device and simulator!
Original answer
Also you can try this,
You needed to import the -Swift.h for for both the framework and the app target
For Example :
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <Foundation/Foundation.h>
#import "XLPagerTabStrip-Swift.h"
#import "RealmSwift-Swift.h"
...... // Add all frameworks, subclasses, and dependance ios frameworks
#import "MyProject-Swift.h"
You can read this article How to import file header and check paths

Chartboost and swift integration

Ok, this is the first time I am trying to integrate chartboost and swift, so I will start from the beginning, maybe I missed something. First I have imported chartboost framework into my project, and then manually created the .h file, which I named BridgingHeader.h I added the needed import statements so that file looks like that now:
#ifndef appname_BridgingHeader_h
#define appname_BridgingHeader_h
#import <Chartboost/Chartboost.h>
#import <Chartboost/CBNewsfeed.h>
#import <CommonCrypto/CommonDigest.h>
#import <AdSupport/AdSupport.h>
#import <UIKit/UIKit.h>
#endif
I tried to build, everything went fine, but I noticed that it does not actually import this file. Hence I went to my project build settings, located Swift Compiler - Code Generation and found Objective-C Bridging Header and named it as appname/BridgingHeader.h . Now I am sure it did find the file and tried to import chartboost files, because I got 56 errors, of which all are in the chartboost files. Erros are such as "function definition is not allowed here" or "expected a type". The last error says that it failed to import bridging header. Anyone knows or has any ideas how to make this work?
Thanks
Ok, I my problem.
1) UIKit import should be done as follows:
#import <UIKit/UIKit.h>
#ifndef appname_BridgingHeader_h
#define appname_BridgingHeader_h
#import <Chartboost/Chartboost.h>
#import <Chartboost/CBNewsfeed.h>
#import <CommonCrypto/CommonDigest.h>
#import <AdSupport/AdSupport.h>
#endif
2) for the path, I should just went to the bridging file, in the file inspector copy the full path and paste it in the Swift Compiler - Code Generation.

cannot find interface declaration for 'NSObject', superclass of 'GPXType'

I have done some research on that issue , but I have not found anything similar just yet.
I am using iOS GPX framework to draw the path on map using GPX file. I have import iOS GPX.framework on my project. but I have face an issue.
Please Guide me, If anyone has any advice...
Just modify the header file, add this line on top of the file
#import <Foundation/Foundation.h>
Seems they thought that you will have a PCH file, where Foundation and UIKit will be imported, but Xcode 6 removed PCH default support, so the problem came. (See my previous answer)
Finally I have solved my problem
I have import #import < UIKit/UIKit.h> and change my Xcode 6 Architectures $(ARCHS_STANDARD_32_BIT).
Thanks so much Guys.
You haven't imported the header file #import ...
When compiling for both iOS and OsX, I had similar issue that I have resolved by importing TargetConditionals.h. The final thing looks like this:
#import <TargetConditionals.h>
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#elif TARGET_OS_MAC
#import <Cocoa/Cocoa.h>
#endif
#interface MyClass : NSObject
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
// Define UIKit methods
#elif TARGET_OS_MAC
// Define Cocoa methods
#endif
- (void)reloadRegisteredComponents;
#end

How can I declare a config.h file, add it to my App-Prefix.pch, and then use it globally in my app?

I want to create a Config.h file to house all my static const strings that should be global to my application.
I've created a new Config.h file, but there are a few things I'm unaware of.
1) How do I declare variables. A or B?
A)
#define hotelURLString4 = #"http://blah.herokuapp.com/api/v1/hotels/";
B)
static NSString * const hotelURLString2 = #"http://blah.herokuapp.com/api/v1/hotels/";
2) I can't seem to use this file. If I try to import the Config.h directly into a file of mine, I get a "Config.h file not found" error in xcode. If I include it in my AppName-Prefix.pch up at the top via...
#import <Availability.h>
#import "Config.h"
#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
That does not seem to work either. How do I do this.
3) How do I access the variable declared/defined. Do I call Config.hotelURLString, or [Config hotelURLString]... or how do I access it?
===================================================================
============================ UPDATE ===============================
1) I created my header like this... am I not doing something correctly, because I tried again and it won't work either.
2) This is my AppName-Prefix.pch file.
#import <Availability.h>
#import "Config.h"
#import "MyHeader.h"
#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
3) Here is my Project Navigator
To answer question 1, method B is probably better, although you should name your variable more like:
kHoteUrlString2
And preferably something more descriptive then just the numbers, but the main point here is the lower case k and the uppercase first letter. This is a C/ObjC naming convention for constants.
As for question 2, it sounds like your file isn't actually in the project. Some more details are needed to answer this part of the question, and I'll update my answer if the question has more details added.
As for question 3, you use the variable exactly as you would as if you had declared it at the top of whatever file you're using. Objective-C doesn't have namespaces.

OpenCV compiler error when using Stitcher under iOS

Whenever I try to use the OpenCV Stitcher class in iOS and I include the stitcher-header ( #include ) I end up with a compile error "Expected '{'" in exposure_compensate.hpp. Apparently the line
enum { NO, GAIN, GAIN_BLOCKS };
is causing some sort of error.
I am very new to openCV but using other functions like filter2d() work as expected. How can I resolve this?
Try
#import <opencv2/opencv.hpp>
then
#import <UIKit/UIKit.h>
Update: this answer only highlights the bare minimum fix of the problem, and perhaps the root cause: order of dependencies. Please refer to other answers for better code / setup that you put in your project.
In your project, create a Prefix Header, MyProject.pch, and set it in your project's build settings.
Then within that pch file, do something like this:
#ifdef __cplusplus
# include <opencv2/opencv.hpp>
# include <opencv2/stitching/detail/blenders.hpp>
# include <opencv2/stitching/detail/exposure_compensate.hpp>
#else
# import <Foundation/Foundation.h>
# import <UIKit/UIKit.h>
# import <Availability.h>
#endif
I also ran into this problem. As G. Führ ensure you include the opencv headers first. The easiest way to do this is add:
#ifdef __cplusplus
#include <opencv2/opencv.hpp>
#endif
near the top of the apps "Appname-Prefix.pch" header. This is a precompiled header and makes it easy to guarantee that your opencv header will be included before any of the apple headers.
//
// Prefix header
//
// The contents of this file are implicitly included at the beginning of every source file.
//
#import <Availability.h>
#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif
#ifdef __cplusplus
#include <opencv2/opencv.hpp>
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
This means you won't accidentally include an apple header before this anywhere else in the app.
I had a similar issue and I solved editing directly the opencv-framework files involved (in your case compensate.hpp) and comment from them the definition of the NO enum case definition. In this case the file was blender.hpp, but compensate.hpp has the same structure
Hope this helps
I solved this issue by importing any Apple headers before those of OpenCV, as mentioned in the beginning of the header:
#if defined(NO)
# warning Detected Apple 'NO' macro definition, it can cause build conflicts. Please, include this header before any Apple headers.
#endif
Hope that helps.
In my case, I create a vertical image stitching app with openCV, error show as screenshot below. It is from exposure_compensate.hpp and blenders.hpp. From the error description, the top file is ../CVWrapper.mm, which is in my project instead of openCV pod project.
As above guys said, there is some conflict issue between C++ and Apple MACRO. And we should put C++ header above Apple header.
First, I try a workaround from internet, which said "replace NO with NO_EXPOSURE_COMPENSATOR = 0". This worked, but it modified openCV source code, I don't want to do that because I will not do version control on Pod files, then if other guys clone my repo/project, they will need to do the same modification on those source code.
Then, I follow the error message in Xcode, I did following changes in my CVWrapper.mm file. After that, those two error disappear.
// Before change
#import "CVWrapper.h"
#import "UIImage+OpenCV.h"
#import "stitching.h"
#import "UIImage+Rotate.h"
// After change
#import "stitching.h"
#import "CVWrapper.h"
#import "UIImage+OpenCV.h"
#import "UIImage+Rotate.h"

Resources