2 Answers
2

There is, obviously, no such language as C++/CLI on Mac OS. On Windows, C++/CLI actually compiles as managed code ran by the CLR, that runs native code; since on Mac OS Mono isn't integrated to the system, it's rather the other way around. Your app is native, and it can host managed code.

Mono exposes functions to host a CLR virtual machine inside a process. Since CLR classes aren't directly exposed to your C code, you'll be able to call methods of objects through reflection-like calls.

If you don't find this very appealing, you may want to go the other way around, as you mentioned, and look into MonoMac, which provides .NET bindings to a large portion of the APIs you may want to use in a Mac application (Cocoa, CoreImage, CoreAnimation, etc) and means to create your own bindings.

Thanks. This seems like a possible way. But it is not simple :(
– Filip KuncApr 18 '11 at 6:33

2

@Filip Kunc Yeah, I know. People always complain over C++/CLI, even though it's actually a great way to mix managed and native code. Still, aside from being quite verbose, embedding Mono is not that bad considering most of those calls are performed only once (you'll be reusing the MonoDomain, MonoAssembly, MonoImage, MonoClass and MonoMethod handles). If you've already dealth with a high-level language's runtime functions (which is a big if), there's nothing quite surprising here.
– zneakApr 18 '11 at 12:51

If you're doing this on Mac, then yes, it's possible. On iOS not so much.

On the Mac, if you can create a CLI app in MonoMac, then you can call your CLI app from within your Objective-C app using NSTask. NSTask allows you to easily launch a commandline tool and then capture it's output and interact with it. To do this, you'd do something like:

Typically, the way you want to do this, is you include the CLI app inside your app bundle. You can then get the path to the CLI app using NSBundles -pathForResource:ofType: method.

iOS doesn't include the NSTask API, so this isn't possible there. I've heard of some folks using MonoTouch to make iOS apps using C#, but as you suggested, I think you're best off sticking with Objective-C for the bulk of your app if possible. Using a CLI app like you describe is definitely an option on the Mac and can be particularly useful when you have a body of code that's already written and tested and works that you just want to "Wrap" with a Cocoa GUI.

So, NSTask is one way to do this using an external CLI executable wrapped in your app bundle. On the other hand, you might be wondering, can you link your C# code directly into Objective-C?

Well, Objective-C is a superset of C, and as such, it has all the capabilities of C. Additionally, if you use Objective-C++ it has all the capabilities of C++ as well. So, IF you can get MonoMac to generate either a C or C++ static library, then yes, you could even just link your static library with your Objective-C cocoa code, and it'll just work. I can't tell you how to make the library from MonoMac, but linking it in is just a matter of adding it to your linked libraries in your build settings in Xcode.

EDIT: I'm not familiar with C++/CLI as a language, and I misinterpreted the meaning of C++/CLI to mean "C++ Command Line Interface app". That said... the techniques I described still apply as possible methods to do what you want to do, but it is NOT using the C++/CLI interface like you can on Windows.