View Full Version : Making ObjC and C++ communicate
Hi all,
I'm trying to make Objective C and C++ play nice with each other. Basically, I want to expose a few Objective C methods in a C++ file (or the other way around, if that would be easier).
I don't have any example code to give, because I haven't begun the project yet. I was just hoping that someone has done this before. I don't need to use both languages in the same source file (http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_4_section_10.html), I just need to be able to expose the methods of one into the other.
Thanks very much in advance.
Okay, now this is tricky, so listen carefully...
Just try it.
:)
Plop your code into a file with an .M or mpp extension, and it will get compiled as Objective-C++, where both styles will be recognized.
ie, in an Obj-C++ class, you can make, call into, and destroy C++ objects, and vice versa. Nothing special needed.
The site I linked to mentioned .mm, not .m or .mpp, so is there one you would recommend? I was hesitant to do it this way, because I couldn't believe it was as simple as embedding both in the same file.
What I actually need to do is pass several ObjC Cocoa methods as parameters in a C++ method. Are you really saying that I could simply write the ObjC method names directly into the C++ method?
Whoops, sorry, yes, mm is correct.
So essentially you want to pass function pointers to the Obj-C methods, as parameters to a C++ method? You're going to want to use the SEL (selector) mechanism of Objective-C. It converts a string to a callable method. Someone else can correct me if I'm wrong (again), but that returned function should be massagable to a C++ style function pointer.
Worst case scenario, pass the string into the C++ function, and use SEL inside it, ie, push a little Obj-C into the C++ function, and make it Obj-C++. Be sure to change the file extension.
And yes, you really can just plop them together.
So essentially you want to pass function pointers to the Obj-C methods, as parameters to a C++ method?
Exactly.
You're going to want to use the SEL (selector) mechanism of Objective-C. It converts a string to a callable method.
Sounds good, I wonder if this is documented somewhere? I've done a quick search online but haven't found much.
From: http://www.faqs.org/faqs/computer-lang/Objective-C/faq/
5.3 How do I know the SEL of a given method ?
If the name of the method is known at compile time, use @selector :
[myObject perform:@selector(close)];
At runtime, you can lookup the selector by a runtime function that
takes the name of the message as argument, as in :
SEL mySel = sel_getUid(name); // for Apple
Look just below that for info on IMP, the other half of the equation. :)
This is a good rundown too: http://www.macdevcenter.com/pub/a/mac/2002/05/24/runtime_partone.html
Surprisingly, the Wikipedia page for it has a nice biblio at the bottom with some rather canonical references: http://en.wikipedia.org/wiki/Objective-C#Objective-C.2B.2B
Oh cool, I was actually at that site earlier for another reason. You've been a great help, thanks so much!
vBulletin® v3.6.4, Copyright ©2000-2024, Jelsoft Enterprises Ltd.