Can I use C++ With Cocoa

Discussion in 'Mac Programming' started by Techguy172, Jun 13, 2007.

  1. Techguy172 macrumors 68000

    Feb 2, 2007
    Ontario Canada
    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

  2. coastertux macrumors regular

    Jul 17, 2006
    I believe you need to use Objective-C...someone correct me if I'm wrong.
  3. Legolamb macrumors 6502a


    Nov 27, 2006
    North of where I'd like to be
  4. dethl macrumors regular

    Aug 28, 2002
    Austin, TX
    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).
  5. MongoTheGeek macrumors 68040


    Sep 13, 2003
    Its not so much where you are as when you are.
    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.
  6. bbarnhart macrumors 6502a


    Jan 16, 2002
    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.
  7. Techguy172 thread starter macrumors 68000

    Feb 2, 2007
    Ontario Canada
    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
  8. psingh01 macrumors 65816

    Apr 19, 2004
    yes, you can use java for that.
  9. Soulstorm macrumors 68000


    Feb 1, 2005
    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.
  10. tohihaho macrumors newbie

    Sep 3, 2007
    How about using cpp files in already built cocoa application ... can I use both languages - objective c and c++ in one project ?
  11. kainjow Moderator emeritus


    Jun 15, 2000
    Check out this old thread, which discusses C++ and Cocoa.
  12. Nutter macrumors 6502

    Mar 31, 2005
    London, England
    Sorry to be pedantic, Mongo, but I'm going to be anyway. CFString is neither C++ nor Carbon!
  13. coren2000 macrumors member

    May 16, 2007

    Maybe QT will be more to your liking.
  14. weg macrumors 6502a


    Mar 29, 2004
    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, "Adding a GUI to a 64-bit Application")
  15. Krevnik macrumors 68040


    Sep 8, 2003
    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.
  16. billtubbs, Aug 14, 2012
    Last edited: Aug 14, 2012

    billtubbs macrumors newbie

    Jan 10, 2009
    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?
  17. bbarnhart macrumors 6502a


    Jan 16, 2002
    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.
  18. lloyddean macrumors 6502a

    May 10, 2009
    Des Moines, WA
    The IDE uses the files extension to determine which compiler (settings).

    C '.c'
    C++ '.cpp'
    ObjC '.m'
    ObjC++ '.mm'
  19. gnasher729 macrumors P6


    Nov 25, 2005
    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

    - (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.
  20. adildacoolset macrumors 65816

    Sep 5, 2011
    Lusaka, Zambia(If you know where it is)
    Use something called Qt. It's for C++ and very capable. You can find it at


    I'm sorry bu guys, we're knocking on a thread from 2007!
  21. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    CFString is part of Core Foundation (hence the CF) and it most certainly is not deprecated.

Share This Page