Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using @import in objective C in conjunction with __cplusplus

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?

like image 251
Mihai Popa Avatar asked Dec 20 '13 16:12

Mihai Popa


People also ask

Can you use C++ in Objective-C?

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.

Is Objective-C compatible with C?

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.


1 Answers

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:

  • Xcode - 11.3
  • CocoaPods - 1.9.1
  • OpenCV2 - 4.3.0
like image 136
MMSousa Avatar answered Oct 30 '22 22:10

MMSousa