🔒 Locked
Calling Objective C code within C++ using Cocoa
Advertisement
Original Post
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 :)
You should be able to use the syntax directly using Objective-C++ (basically Objective-C ∪ C++). See this site for details.
See this thread:
Portability (#ifdef considered harmful)
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.
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.
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
Error's like this are ususally from fortetting to import a header. or linking to the wrong framework
Cheers
Chris
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.
For this to work I also had to change to .mm extention.
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]
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]
Quote:This is standard GCC behavior but you can get around it with certain flags.
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.
Quote: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.
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.
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
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
Thanks for the advice. Since im going to the extra trouble to write for both platforms, I might as well bite the bullet and do two files.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement