f



How to Call Objective C Object from C++ code

Hi, guys:

i prepare to build UI of program by cocoa.
but in order to make a portable program, some low level code sould
write in C++. then i must find some way to call Apple Service or
Objective C from C++.
Could anyone tell me a way to do that.

Best Regard.

0
kingdry_cn (10)
3/31/2006 6:00:17 AM
comp.sys.mac.system 33446 articles. 2 followers. jfmezei.spamnot (9455) is leader. Post Follow

6 Replies
1115 Views

Similar Articles

[PageSpeed] 37

In article <1143784817.800849.152730@t31g2000cwb.googlegroups.com>,
 "Mopelee" <kingdry_cn@sina.com.cn> wrote:

> Hi, guys:
> 
> i prepare to build UI of program by cocoa.
> but in order to make a portable program, some low level code sould
> write in C++. then i must find some way to call Apple Service or
> Objective C from C++.
> Could anyone tell me a way to do that.

In general you just make the call to the objective-C method.

However you do need to make sure that the source code is being compiled 
as objective-C or objective-C++ for this to work.  That's automatic for 
a ".m" or ".mm" file, otherwise you can change the language used in the 
project settings.

-- 
Tom "Tom" Harrington
Macaroni, Automated System Maintenance for Mac OS X.
Version 2.0:  Delocalize, Repair Permissions, lots more.
See http://www.atomicbird.com/
0
tph (2302)
3/31/2006 5:06:31 PM
thank u, Tom.
  I know how to call objective c from objective c++,just like call from
..mm to .m.
  but i wanna find some way to to call from .cpp to .m
  anyone have idea?

0
kingdry_cn (10)
4/7/2006 7:54:23 AM
In article <1144396463.858577.245830@e56g2000cwe.googlegroups.com>,
 "Mopelee" <kingdry_cn@sina.com.cn> wrote:

> thank u, Tom.
>   I know how to call objective c from objective c++,just like call from
> .mm to .m.
>   but i wanna find some way to to call from .cpp to .m
>   anyone have idea?

Normally Xcode will compile a .cpp as C++, and won't recognize 
Objective-C method calls.  However for any file you can override the 
defalult and tell Xcode to treat it differently.  You do this by 
selecting the file in Xcode and opening the inspector window.  Under the 
"File type" setting, choose the appropriate option.  From your 
description I think you'd want "sourcecode.cpp.objcpp".

-- 
Tom "Tom" Harrington
Macaroni, Automated System Maintenance for Mac OS X.
Version 2.0:  Delocalize, Repair Permissions, lots more.
See http://www.atomicbird.com/
0
tph (2302)
4/7/2006 4:38:04 PM
"Mopelee" <kingdry_cn@sina.com.cn> writes:
>
>   I know how to call objective c from objective c++,just like call from
> .mm to .m.
>   but i wanna find some way to to call from .cpp to .m
>   anyone have idea?

Objective C is C, plus the object/method extensions.

You can't make an Objective C method call from C++ code, but you can
call an ordinary function.  So should be able to make a simple Objective
C source file (.m extension) containing a C function that makes the
method call.  Declare the prototype for that function (in an extern "C"
block) in your C++ file and call it.

At least I would expect this to work.  Haven't actually tried it.

You might prefer to ask on one of the comp.sys.mac.programmer
newsgroups.  They will probably be able to give you better answers.

-- David
0
shamino (1254)
4/8/2006 8:46:53 PM
In article <1144396463.858577.245830@e56g2000cwe.googlegroups.com>,
 "Mopelee" <kingdry_cn@sina.com.cn> wrote:

> thank u, Tom.
>   I know how to call objective c from objective c++,just like call from
> .mm to .m.
>   but i wanna find some way to to call from .cpp to .m
>   anyone have idea?

I think you're looking for the objc_msgSend() function. I haven't used 
it so I don't know all the ins and outs.
0
invalid163 (956)
4/9/2006 5:27:22 AM
Mat Cvetic <invalid@invalid.net> writes:
>
> I think you're looking for the objc_msgSend() function. I haven't used
> it so I don't know all the ins and outs.

Note, however, that this is not portable.  From the top of the
documentation page where it is described:

        The Mac OS X implementation of the Objective-C runtime library
        is unique to the Mac OS X platform. For other platforms, the GNU
        Compiler Collection provides a different implementation with a
        similar API. This document covers only the Mac OS X
        implementation.

So, if you think you may need to bridge Objective C with C++ on some
other platform (remember that gcc supports Objective C on many
platforms), you should use the mechanism I described - writing your own
Objective C function with a C interface and use that as the bridge.

-- David
0
shamino (1254)
4/9/2006 4:47:04 PM
Reply: