c++ - together - Mixing Objective C,(*.m,*.mm &.c/.cpp)files




wrap c++ in objective c (3)

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.

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>

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.


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




objective-c++