PDA

View Full Version : XCode - C++ with obj-C examples??




Oats
May 22, 2006, 08:04 PM
Hi,
I am looking to develop an applicate for OS X with Apple's XCode. I have a few classes that I have defined for Visual C++, but they should be fairly cross-platform. So I would like to use those C++ classes, but my impression is that the GUI builder and everything else is built to use obj-C. Are there any examples of mixing these two languages into the same app?



gekko513
May 22, 2006, 08:12 PM
I believe you can start a Carbon project to use C++. Carbon has its own set of GUI widgets in Interface Builder.

If you want to use Cocoa that has more features and integration when it comes to Interface Builder, I think you're limited to Objective-C and plain C.

As far as I can see, you have three choices:
1. Make your application using the Carbon framework
2. Rewrite your classes in Objective C. (It shouldn't be that hard)
3. Write some sort of bridge in plain C to access functionality in your C++ classes.

Maybe someone else can offer better solutions...

Catfish_Man
May 22, 2006, 08:59 PM
As far as I can see, you have two choices:
1. Make your application using the Carbon framework
2. Use Objective-C++ and mix ObjC and C++ in the same app (just use a .mm suffix instead of .m on your files)


Fixed :)

gekko513
May 22, 2006, 09:01 PM
Fixed :)
That's cool. I didn't know you could do that.

Catfish_Man
May 22, 2006, 09:06 PM
That's cool. I didn't know you could do that.

It required quite a bit of compiler work on Apple's part. iirc the official version of GCC either just gained objc++ support, or will be soon.

Oats
May 22, 2006, 11:47 PM
so thats sounds pretty easy... there's nothing more to it? just rename my .cpp files to .mm?

i will also have to learn how to call my c++ classes with obj-c, but that should be pretty straightforward???

GeeYouEye
May 23, 2006, 01:33 AM
so thats sounds pretty easy... there's nothing more to it? just rename my .cpp files to .mm?

i will also have to learn how to call my c++ classes with obj-c, but that should be pretty straightforward???

Your C++ classes are just that. You shouldn't need to mess with them if they're model (business) objects. Just #include the headers in your Obj-C++ .mm files, (and to the .h if a C++ instance is a data member). Then just instantiate and use as normal. The runtimes are totally separate... it's just a code-level thing; don't pass a C++ string where an NSString * is needed.

Oats
May 23, 2006, 08:26 AM
Just #include the headers in your Obj-C++ .mm files, (and to the .h if a C++ instance is a data member).
What the heck is Obj-C++??? Are those files which are a hybrid of Obj-C and C++?

gekko513
May 23, 2006, 09:03 AM
What the heck is Obj-C++??? Are those files which are a hybrid of Obj-C and C++?
Syntactically, Obj-C is in essence regular C with a particular extension for object oriented programming. You can program regular C in Obj-C.

I'm pretty sure that Obj-C++ is regular C with the C++ and the Objective-C extensions. Thus Obj-C lets you do object oriented programming in two different ways.

Soulstorm
May 24, 2006, 01:15 AM
If you want to program fot OS X proffessionally, you will choose among the Carbon and Cocoa API. The Carbon API is accessible through plain C and C++ and the Cocoa API is accessible through Java, Objective-C and Applescript. The greatest flexibility is offered by Objective-C.

I tried the Carbon API, but I disliked it. It required much effort just to make a simple program that with Cocoa could do in a matter of minutes. Objective-C offers you many more capabilities at a smaller amount of time. You can still make a proffessional quality program with Carbon, though.

Objective C++ is a language frontend to the Objectiive-C language. It allows integration of of Objective-C and C++ in the same files, with some limitations considering the inheritance of the classes. Obj-C++ is Apple's thing, though. It doesn't exist on GCC for windows, if I'm not mistaken. I don't know if it will eventually be supported in Windows, but it is a very good solution for Mac programmers who want to take advantage of Objective-C's productivity and Cocoa development with the flexibility and low-level power that C++ gives.