15

In my project Core libraries are part of C/C++ files, while UI needs to be developed in Objective C, I am able to access/Call C++ functions from Objective C/.mm files but reverse no luck so far, i.e. i am not able to call Objective C functions from C++ Files, when i tried to include Objective C header even system header

#import <foundation/foundation.h> 

getting around 1000+ compilation error,

something like this

/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:180:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:180: error: expected unqualified-id before '@' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:182:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:182: error: expected initializer before '*' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:183:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:183: error: 'NSString' was not declared in this scope


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:183:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:183: error: 'aSelectorName' was not declared in this scope

Am i missing some pre-compile flag etc.. can anyone suggest me, the best possible way to call/access objective C class which is inherited from NSObject, without modifying much C++ code, i just need to call one function

Code structure / Order to include header files are

Some system header file
Some Core Class Header file 

#import <foundation/foundation.h>
3
  • 2
    <foundation/foundation.h> should be <Foundation/Foundation.h>
    – user557219
    Jan 17, 2011 at 15:18
  • @Bavarious: although it will probably work as is because HFS+ is case insensitive by default.
    – JeremyP
    Jan 17, 2011 at 16:08
  • 1
    @JeremyP Yup, hence should instead of must. =) His code will break when compiled in a case sensitive filesystem.
    – user557219
    Jan 17, 2011 at 16:11

5 Answers 5

50

If you have a .cpp file with C++ code that needs to use Objective-C as well, either rename that .cpp file to .mm or pass -x objective-c++ to the compiler.

3
  • 1
    how can we pass this parameter to the compiler? can we find this in project settings?
    – AmineG
    Jun 9, 2011 at 20:20
  • 2
    @some In that case, right-click the file on the project tree, choose Get Info and, in File Type, choose sourcecode.cpp.objcpp. This will tell Xcode to compile that file with -x objective-c++.
    – user557219
    Jun 9, 2011 at 21:44
  • 3
    Using Xcode 4.6 I did this at the project level be setting -x objective-c++ for "Other C++ Flags" and "Other C Flags" under "Apple LLVM Compiler 4.2 - Language" in the Build Settings tab for my project. Oct 29, 2013 at 18:19
4

I found it imposible to use any Objective-c in the C++ header files.

However, you can include Objective-c in the implementation files.

(.mm or you can set how to interpret .cpp files in the info of the file. Choose Info->General:FileType:Sourcecode.cpp.objcpp )

Use

cppClass.h:

class objcClass;

objcClass* mMemberVariable;

cppClass.mm:

#import "objcClass.h";

void cppFunction(){
    [objcClass message];
}

in the cpp header file.

Then include the header that defines the class in the .cpp or .mm file.

2
  • You can use Objective-C in a C++ header file, I am working on a project that does this. The implementation is set as Objective-C++ in the Utilities->File Inspector and the obj-c code within the head is wrapped in #if defined( OBJC )
    – rich.e
    Jan 17, 2012 at 21:25
  • 1
    I found you cannot declare any Objective-C classes in the header file without the file including it being a .mm file. In my case since my code needs to be cross-platform, I declared any Objective-C classes as void* in the header and then cast them in the .mm implementation file.
    – Adamski
    Aug 30, 2015 at 7:38
1

If you use Qt, there is Q_FORWARD_DECLARE_OBJC_CLASS macro which can be used to forward declare an Objective-C class.

As a reference, here is the implementation:

#ifndef Q_FORWARD_DECLARE_OBJC_CLASS
#  ifdef __OBJC__
#    define Q_FORWARD_DECLARE_OBJC_CLASS(classname) @class classname
#  else
#    define Q_FORWARD_DECLARE_OBJC_CLASS(classname) typedef struct objc_object classname
#  endif
#endif
0

I used a technique of the name expand files depending on a programming language. Also, I added "-x objective-c ++" to the compiler, but the problem remained. I was helped by council for the link, pay attention to the "file type" parameter.

0

In XCode 11, it can be done by changing the build setting Compile Source as (GCC_INPUT_FILETYPE) to Objective-C++ (sourcecode.cpp.objcpp). By doing this, there is no need to change the file extension type to mm.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.