Jump to content
  • Advertisement
Sign in to follow this  
FruitJuice27

Calling Objective C code within C++ using Cocoa

This topic is 4954 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!