# Cocoa/Obj-C++ with C++ based plugin API?



## kuroyume (Oct 24, 2008)

In the latest version of the company's software in which I develop C++ cross-platform plugins, they now require MacOS 10.5 and Xcode 3.1 in order to build a multi-target, 32/64-bit dylib plugin for Mac.  I've already asked the company how I would go about using Cocoa instead of Carbon for certain requirements but they directed me to Apple.  So, here I am. 

I can't change the target or build types - whatever project settings there are are REQUIRED for the plugin dylib to work in the target application.  But if a slight setting that 'turns on' Obj-C/C++ works (it might already be there but am unsure), then it may be possible.  I don't know.  Everything talks Cocoa app this, Cocoa app that wherein Cocoa is the star and C++ is mitigated to a support position.  Sorry, I can't 'switch' to Cocoa since this is a C++ plugin API - verstand?  C++ is the star and Cocoa needs to be supportive only.

So what steps would be needed to get an Obj-C/C++ code integrated into my C++ dylib project.  How would one go about including Obj-C/C++ code/frameworks/whatever in a C++-specific project in Xcode?

Thanks,
Robert


----------



## Mikuro (Oct 24, 2008)

I can't say exactly what will work, since it sounds like you have more specific requirements than I've dealt with. However, here's the way I've done it:

First, in the Project menu, select Add to Project, and add /System/Library/Frameworks/Cocoa.framework. Then, rename one or more ".cpp" files to ".mm" to tell the compiler to treat it as Objective-C++ (the header files can be left as-is, which is why I think it will still fit into the plugin API). Add "#import <Cocoa/Cocoa.h>" to the top of the .mm file, and then you can use Objective-C/Cocoa anywhere in that file. You should probably avoid #importing Cocoa.h from your .h files and just do it in the .mm files.

You must take care to manage your own autorelease pools, since your program will not be operating in the Cocoa application loop, which normally creates at least one pool automatically for you. In short, the first thing you should do in any function where you use Objective-C is "NSAutoreleasePool *pool = [NSAutoreleasePool new];", and the last thing you should do in that function is "[pool release];".


----------

