Sign in to follow this  
FruitJuice27

Calling Objective C code within C++ using Cocoa

Recommended Posts

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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Quote:
Original post by chollida1
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.
This is standard GCC behavior but you can get around it with certain flags.

Quote:
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.
Create a C/C++ interface for creating a message box, then create two implementation files. One will be an .mm and one will be a .cpp. You can then set up your project so that the Windows version compiles and links with the the Windows implementation and the MacOS version compiles and links with the Mac implementation. Your code calls the wrapper function. No #ifdefs necessary.

Share this post


Link to post
Share on other sites
We use .mm. Vs.net will compile .mm file if you set up a file association for them. Just look in the registry for .cpp and copy that key for .mm.

Works well for us. igni ferroque mentioned that you can use certain compiler flags to get cpp files to work as ObjectiveC++ files but we've never had any luck getting it to work by setting the compile as ObjectiveC++ flag.

And judging by the XCode mailing list we aren't alone

Cheers
Chris

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this