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

Techguy172

macrumors 68000
Original poster
I would like to know if its possible to C++ to code an application using the Cocoa Programming or do i have to use C

Thanks
 
A few years ago I wrote a hybrid Obj-C/C++ application. It was a port of a final project I did one of my early CS classes. I used Obj-C to handle GUI events and kept the C++ basically the same. The big problem with Obj-C that I ran into was that file i/o cannot handle C++ strings. I had to go on a character by character basis (I could have used character arrays but this was faster).
 
A few years ago I wrote a hybrid Obj-C/C++ application. It was a port of a final project I did one of my early CS classes. I used Obj-C to handle GUI events and kept the C++ basically the same. The big problem with Obj-C that I ran into was that file i/o cannot handle C++ strings. I had to go on a character by character basis (I could have used character arrays but this was faster).

It depends on what type of strings you used. CFString (a C++ Carbon class) is toll free bridged to NSString (a cocoa Obj-C class) and you can use the two interchangeably.

I have code that does just that.
 
I have a project that has CPP files and Cocoa in it. I don't remember what you have to do to get it to work besides rename the Cocoa files with a *.mm instead of *.m.
 
Ok so basicaly im stuck with objective C, This leaves me a problem I want to build an application that works universaly with all operating systems but it looks like im going to have to rewrite it if i want to. Is this true
 
Ok so basicaly im stuck with objective C, This leaves me a problem I want to build an application that works universaly with all operating systems but it looks like im going to have to rewrite it if i want to. Is this true

yes, you can use java for that.
 
A few years ago I wrote a hybrid Obj-C/C++ application. It was a port of a final project I did one of my early CS classes. I used Obj-C to handle GUI events and kept the C++ basically the same. The big problem with Obj-C that I ran into was that file i/o cannot handle C++ strings. I had to go on a character by character basis (I could have used character arrays but this was faster).

You could easily convert the std::strings to NSStrings and use the Cocoa output. Or, convert the NSSTrings to std::strings and use the standard C++ output.
 
How about using cpp files in already built cocoa application ... can I use both languages - objective c and c++ in one project ?
 
Qt

Ok so basicaly im stuck with objective C, This leaves me a problem I want to build an application that works universaly with all operating systems but it looks like im going to have to rewrite it if i want to. Is this true

Maybe QT will be more to your liking.
 
Ok so basicaly im stuck with objective C, This leaves me a problem I want to build an application that works universaly with all operating systems but it looks like im going to have to rewrite it if i want to. Is this true

It's probably a good idea to keep the UI separate from the actual application (if you're developing an application for Apple's "48-bit" Operating System Tiger, then that's the only option anyway).
Keep the actual application in a separate process, and exchange data with the UI process using IPC, shared memory, or something similar.

(see http://developer.apple.com/macosx/64bit.html, "Adding a GUI to a 64-bit Application")
 
It's probably a good idea to keep the UI separate from the actual application (if you're developing an application for Apple's "48-bit" Operating System Tiger, then that's the only option anyway).
Keep the actual application in a separate process, and exchange data with the UI process using IPC, shared memory, or something similar.

(see http://developer.apple.com/macosx/64bit.html, "Adding a GUI to a 64-bit Application")

There are easier ways to keep a clean OM/UI separation without having to run two processes.

If you have a clean library with externally-linkable C functions, you can use that for the object model, and then just wrap Obj-C around it/etc... C++ gets trickier, but can be done as well. Link this platform-neutral library statically (or even dynamically) with the UI host and you don't get the overhead of IPC.
 
Adding C++ to a Cocoa app in XCode

Sorry if this is a dumb question, but many of the answers to the original question talk about building apps using Objective-C++ so that you can include C++ routines while also using the Cocoa framework for the GUI. But how do you create an Objective-C++ app with XCode? I'm using XCode 4.4.1 with OS X 10.7.4. I can see how to create a new C++ command line tool but I can't see a Cocoa application template that uses Objective-C++.

I realise this thread is getting old. Am I missing something or what has changed with XCode?
 
Last edited:
Not sure what you want to do, but you can use a C++ file with a regular Objective-C project. Add the C++ file to the project then change the extension to .mm and that should be it.

If you're wanting something else let me know.
 
Sorry to be pedantic, Mongo, but I'm going to be anyway. CFString is neither C++ nor Carbon!

Sorry to be pedantic, but CFString is Carbon; you can use it in C, C++, Objective-C and Objective-C++, but it is really deprecated.

You can just mix std::string and NSString*, and add a category written in Objective-C++ with two methods

Code:
- (std::string) asStdString;
+ (NSString*) stringWithStdString:(const std::string&) stdString;

And there is no such thing as an Objective-C++ project. Just create a Cocoa project, then add files with suffix .c (C), .m (Objective-C), .cpp (C++) or .mm (Objective-C++) as you feel fit. Source files that use both C++ objects and Objective-C objects just end in .mm.
 
Ok so basicaly im stuck with objective C, This leaves me a problem I want to build an application that works universaly with all operating systems but it looks like im going to have to rewrite it if i want to. Is this true

Use something called Qt. It's for C++ and very capable. You can find it at http://qt.nokia.com/

----------

I'm sorry bu guys, we're knocking on a thread from 2007!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.