Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Oats

macrumors regular
Original poster
Jan 8, 2003
194
1
New York
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

macrumors 603
Oct 16, 2003
6,301
1
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

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
gekko513 said:
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 :)
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
gekko513 said:
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

macrumors regular
Original poster
Jan 8, 2003
194
1
New York
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

macrumors 68000
Dec 9, 2001
1,669
10
State of Denial
Oats said:
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

macrumors regular
Original poster
Jan 8, 2003
194
1
New York
GeeYouEye said:
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

macrumors 603
Oct 16, 2003
6,301
1
Oats said:
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

macrumors 68000
Feb 1, 2005
1,887
1
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.