• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Calling an Objective-C function from C++?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Calling an Objective-C function from C++?


  • Subject: Re: Calling an Objective-C function from C++?
  • From: Charles Srstka <email@hidden>
  • Date: Tue, 31 Jan 2012 14:56:14 -0600

On Jan 31, 2012, at 1:31 PM, Howard Moon wrote:

> 	How does one call a Cocoa function from C++ code?
>
> 	I've got an existing .cpp file that leverages the vstgui framework, which includes support for Cocoa in the underlying layers.  However, vstgui does not support dialogs or alerts. My code already supports native Windows dialogs/alerts, and Carbon windows/alerts, as needed, and I'd like to add support for Cocoa when my audio plug-in is compiled as 64-bit on the Mac.
>
> 	One of my current functions is something like this:
>
> void MyObj::showAlert( char* text, char* caption )
> {
> #ifdef WIN32
> 	// calls ShowMessage
> #else
> 	// calls CreateStandardAlert and RunStandardAlert
> #endif
> }
>
> 	I'd like to add the ability to support Cocoa, by adding a .mm/.h file with the Cocoa code to display an NSAlert, and call it from here when the preprocessor symbol MAC_COCOA is defined.  I've created the Cocoa files, but how can I include and call into that code from my .cpp file?  Simply adding either #import or #include of my new .h file causes many many errors, even though the .mm file itself compiles fine.
>
> 	Anyone know of a very simple example showing what I need to do here?  All the examples I find so far show making the call from a .mm file in the first place, but I can't do that because my code is also compiled for 32-bit Carbon (and Windows). How do I call from a .cpp file into a .mm file?

Put this at the top of your .h file:

#ifdef __cplusplus
extern "C" {
#endif

and this at the bottom:

#ifdef __cplusplus
}
#endif

Then, just wrap all the Cocoa method calls you want to make in plain C functions, and restrict all usage of Objective-C to the .m, leaving the .h as plain C.

Charles
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


References: 
 >Calling an Objective-C function from C++? (From: Howard Moon <email@hidden>)

  • Prev by Date: Re: Calling an Objective-C function from C++?
  • Next by Date: Transparent window not getting mouse down events
  • Previous by thread: Re: Calling an Objective-C function from C++?
  • Next by thread: Re: Calling an Objective-C function from C++?
  • Index(es):
    • Date
    • Thread