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

Amitg2k12 picture Amitg2k12 · Jan 17, 2011 · Viewed 33k times · Source

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>

Answer

user557219 picture user557219 · Jan 17, 2011

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.