Calling Objective C code within C++ using Cocoa

Started by
11 comments, last by FruitJuice27 19 years, 5 months ago
Is it possible to call Obj-C code inside C++ code? How would I go about this? For instance i have a MessageBox.cpp that uses the windows MessageBox function if copiling for windows. I would like it to Compile for MAC using the Cocoa NSAlert. I am doing: #if WINDOWS #elif MAC #endif Thanks :)
Advertisement
You should be able to use the syntax directly using Objective-C++ (basically Objective-C ∪ C++). See this site for details.
Free Mac Mini (I know, I'm a tool)
See this thread:
Portability (#ifdef considered harmful)
Thankyou for both your replies!

Petewood - I will keep in mind the swiss cheese code that can come with #ifdef and try to use it correctly. I am staying away from OS API as much as possible (using SDL and OpenGL) so I think that it will be ok for now.

igni - Thanks for the link to that article. It is exactly what I need to solve my issue.

Quote:Original post by FruitJuice27
Thankyou for both your replies!

No problem.
I am not quite understanding Xcode Frameworks. I have added the Appkit framework to my project so that I could use NSAlert.

However my Compiler says that NSAlert is undefined.

NSAlert *alert = [[[NSAlert alloc] init] autorelease];

Is it because im doing something wrong with mixing ObjC and C++? This is a C++ file and is mostly C++ code except for this.
are you importing the framework correctly. We build our apps on C++ code that is shared between the PC and MAC. with the mac app written in Objective C code. We have no problems with it.

Error's like this are ususally from fortetting to import a header. or linking to the wrong framework

Cheers
Chris

CheersChris
Thanks Chris. I did not understand that adding a framework wasnt all I needed. You are right that I was missing the proper headers.

For this to work I also had to change to .mm extention.
Oh yes, I should have reminded you about the .mm extension. I always found it "hacky" that apple would compile a file based on its extension, while the whole OS does its best to sheild the user from the extention of a file.

Glad you got it to work.

Cheers
Chris
CheersChris
Since the extention must be .mm, and my VC++6 wants .cpp, I guess I need different files for each compiler?

I was hoping to have only one file and use:
#if WINDOWS
#elif MAC
#endif

Is there a way to embedd the obj-C code in the cpp file so I can use it with VC++6?

Mabe the best solution is to have two files? What do you do Chris?

[Edited by - FruitJuice27 on November 25, 2004 2:46:05 PM]

This topic is closed to new replies.

Advertisement