When I try to compile an Objective C++ file (.mm) that is linked to a file that uses the new @import directive, I get some errors. Currently, my only solution is to replace the @import with the old #import directive.
Is there any other solution so I can still use @import?
Sometimes, you might want to use a C++ library in your iOS app, or you might want write a part of your code with C++ in order to “cross-platform”. Fortunately, Objective-C works pretty good with C++. This simple tutorial gives you a way to define a new C++ classC++ classA class in C++ is a user-defined type or data structure declared with keyword class that has data and functions (also called member variables and member functions) as its members whose access is governed by the three access specifiers private, protected or public.https://en.wikipedia.org › wiki › C++_classesC++ classes - Wikipedia and use it in Objective-C code.
Objective-C is an object-oriented programming language that is a superset of C, as the name of the language might reveal. This means that any valid C program will compile with an Objective-C compiler. It derives all its non-object oriented syntax from C and its object oriented syntax from SmallTalk.
In my case I needed to use OpenCV in my application.
I have solved this problem by adding
-fcxx-modules
(Objective C++) manually to "Other C++ Flags"
(Build Settings/ Apple Clang - Custom Compiler Flags/ Other C++ Flags)
or
-fmodules
(Objective C) to "Other C Flags"
(Build Settings/ Apple Clang - Custom Compiler Flags/ Other C Flags)
And also I needed to put import the opencv.hpp
before any Apple's headers to fix errors of Expected identifier in opencv library.
For example in Prefix.pch
I organized it that way:
#ifdef __cplusplus
#include <opencv2/opencv.hpp>
#endif
#import <Availability.h>
I am using:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With