Can I use C++ With Cocoa

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

  1. Techguy172 macrumors 68000

    Joined:
    Feb 2, 2007
    Location:
    Ontario Canada
    #1
    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
     
  2. coastertux macrumors regular

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

    Legolamb

    Joined:
    Nov 27, 2006
    Location:
    North of where I'd like to be
    #3
  4. dethl macrumors regular

    Joined:
    Aug 28, 2002
    Location:
    Austin, TX
    #4
    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

    MongoTheGeek

    Joined:
    Sep 13, 2003
    Location:
    Its not so much where you are as when you are.
    #5
    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

    bbarnhart

    Joined:
    Jan 16, 2002
    Location:
    Stilwell, Kansas
    #6
    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

    Joined:
    Feb 2, 2007
    Location:
    Ontario Canada
    #7
    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

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

    Soulstorm

    Joined:
    Feb 1, 2005
    #9
    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

    Joined:
    Sep 3, 2007
    #10
    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

    kainjow

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

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

    Joined:
    May 16, 2007
    #13
    Qt

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

    weg

    Joined:
    Mar 29, 2004
    Location:
    nj
    #14
    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")
     
  15. Krevnik macrumors 68030

    Krevnik

    Joined:
    Sep 8, 2003
    #15
    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

    Joined:
    Jan 10, 2009
    Location:
    Vancouver
    #16
    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

    bbarnhart

    Joined:
    Jan 16, 2002
    Location:
    Stilwell, Kansas
    #17
    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

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

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

    gnasher729

    Joined:
    Nov 25, 2005
    #19
    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.
     
  20. adildacoolset macrumors 65816

    Joined:
    Sep 5, 2011
    Location:
    Lusaka, Zambia(If you know where it is)
    #20
    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!
     
  21. Cromulent macrumors 603

    Cromulent

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

Share This Page