Mac OS X development using C++

Discussion in 'Mac Programming' started by MichaelS, Dec 1, 2007.

  1. MichaelS macrumors newbie

    Joined:
    Dec 1, 2007
    #1
    I'm a Windows developer interested in developing a (graphical) application in C++ to run on Mac OS X. I understand that Cocoa only works with Objective-C, so I would need to use Carbon. However, Apple has stated that it will not provide a 64-bit version of Carbon, at least not for the UI-portion, see http://www.carbondev.com/site/?page=64-bit+Carbon.

    Now, my application doesn't need to be 64-bits at this point (although it would be nice). However, is it at least possible to write a 32-bit native C++ application on Mac OS X that has the same look-and-feel as a 64-bit application written in Objective-C?

    I want my application to feel like a true, first-rate Macintosh application with a modern user-interface (and not look like some application written in the dark ages).

    Also, what are my long-term prospects? When Apple releases e.g. Mac OS X 10.6, will it still be possible to write a state-of-the-art GUI-based application in C++ for it? Or is C++ simply the wrong language for Mac OS X programming?

    Perhaps there is a chance that we will see 64-bit Carbon in Mac OS X 10.5.3 or something, what do people think?
     
  2. gekko513 macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #2
    For almost all applications there's no difference between 32 and 64 bit applications. Certainly none that's visible to the user. As for Carbon, it doesn't have all the native widgets, but most are there, and you can always make your own.
     
  3. MichaelS thread starter macrumors newbie

    Joined:
    Dec 1, 2007
    #3
    Well, I don't really want to get bogged down implementing my own widgets. The operating system is supposed to free me from that task. I just want to know if I have access to all widgets using C++.

    What programming language do people use for theier Mac OS X development? Am I the only one interested in using C++?
     
  4. gekko513 macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #4
    Carbon widgets
     

    Attached Files:

  5. gekko513 macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #5
    Cocoa widgets
     

    Attached Files:

  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    It's possible. Interface Builder 3 makes it much better to create Carbon interfaces. But overall it'll take more work than Cocoa.

    I highly doubt it. Don't count on it at all.

    Objective-C is the "official" language.

    Here's the low down: just because you're a C++ developer doesn't mean you should use Carbon on the Mac because it is available to you. By not including Carbon in the 64-bit goodness, Apple is essentially making Carbon obsolete in the next few years. They already stated they will not be adding as many features to it, mostly just bug fixes. The features go into Cocoa. Yes it will be available in the operating system for quite some time I imagine (probably through 10.6 and 10.7), but there aren't many apps anymore that use it (Adobe and Microsoft are the main ones).

    If you're new to Mac programming (which you are), don't learn Carbon but learn Objective-C/Cocoa. It might be harder in the beginning, but it will pay off in the end.
     
  7. Gelfin macrumors 68020

    Gelfin

    Joined:
    Sep 18, 2001
    Location:
    Denver, CO
    #7
    My suggestion is to download yourself a copy of "The Objective C 2.0 Programming Language" from developer.apple.com and look into the section on "Objective-C++." This allows interoperability between C++ and Objective-C code, subject to certain rules. Assuming you're following a proper MVC division, you should be able to develop your model using C++ and then either put an Objective-C interface wrapper on top of your model or just bridge between languages directly in your controller. Depending on your needs you might be able to get by with a minimum of Objective-C and still have a proper Cocoa application.
     
  8. unknown.exe macrumors member

    unknown.exe

    Joined:
    Sep 22, 2007
    Location:
    Somewhere on Earth
    #8
    You are not the only person using C++ in mac, i am too. But the best thing you can do is to use the graphical interface builder that comes with xcode... then add the .nib file to your program. 32 bit looks like 62 bit... no difference(visably).:apple:
     
  9. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #9
    Am I the only one who has thought on proposing learning Objective C++?

    Since you are a C++ guy, you can use C++ in Cocoa, through Objective C++. Design the foundation of your application using C++, and design the UI using Objective C++. That way, you can use both languages, and Objective C as a wrapper to your C++ core.

    I also do not recommend Carbon. It's just not that flexible and easy. Cocoa development is much faster than Carbon.
     
  10. MichaelS thread starter macrumors newbie

    Joined:
    Dec 1, 2007
    #10
    I would like to thank everybody for their help --- gekko513 for bothering to post screenshots, kainjow for answering all my basic questions, Gelfin and Soulstorm for pointing out the availability of Objective-C++, and unknown.exe for assuring me that I'm not alone :)

    Based on your recommendations, I suppose I will take a look at Objective-C++ and ditch the idea of using Carbon. Not that I was reluctant to use Cocoa, it's just I knew it wasn't available to C++ programmers :(

    I do find it annoying though that Objective-C is in effect the only language Apple actively supports for writing applications using Cocoa. They're really telling us programmers which language to use, and leave us no choice. Not that Objective-C is a bad language, but it's certainly unfamiliar to most programmers with a non-OS X background.

    The reason I dispute this is that I would like to keep my application as platform-independent as possible, and having to use Objective-C makes this considerably harder, obviously. I don't think anybody writes software for e.g. Windows or Linux using Objective-C.

    Any sample code, or literature on this? (Apart from what Gelfin mentions.)
     
  11. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #11
    The other side of the coin is that they don't write applications for Windows or Linux using Carbon either. I'd suggest writing the majority of your application in pure, platform independent, C++ and write loosely coupled UI sections for each platform using whatever technology/toolset works best on that platform, so Cocoa/Obj-C for Mac OSX, .Net (maybe) for Windows, GTK+/KDE for Linux.
     
  12. MichaelS thread starter macrumors newbie

    Joined:
    Dec 1, 2007
    #12
    I appreciate your comment, but then on Windows at least you have a choice of programming language --- it's possible to write modern Windows apps using Basic, C, C++, Object Pascal (Delphi).

    On Mac OS X you have to learn an entirely new language (with a really weird syntax if you're coming from C++).
     
  13. MichaelS thread starter macrumors newbie

    Joined:
    Dec 1, 2007
    #13
    So, what do you think Adobe and MS will do --- rewrite everything in Objective-C just to use Cocoa?
     
  14. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #14
    I imagine they'll do what I suggested above: use their existing code base for all the complex and powerful functionality and just use Cocoa to provide a GUI over the top.
     
  15. Gelfin macrumors 68020

    Gelfin

    Joined:
    Sep 18, 2001
    Location:
    Denver, CO
    #15
    Bite the bullet and give it an honest try. You may very well find you like it, and I know whereof I speak here. Objective-C is an interesting beast.

    The not-so-dirty little secret of Objective-C is that really it's just plain old C with a fancy OO preprocessor and a runtime library. Theoretically you could cook up something functionally similar just using preprocessor macros. Basically it's an API that's unusually tightly coupled in presentation with the language, and the result is something pretty cool. You get the performance of a native compiled language with many of the spiffy runtime tricks of an interpreted language.

    Think about it this way: You learn Win32 and ATL and MFC and .NET and the like to program on Windows and those won't benefit you in the least anywhere else. Picking up Objective-C on the Mac is not really all that different. You're just mentally tripping yourself up over a perceived difference in difficulty between learning an API and learning a language. Knowing C gets you 80% of the way there anyway. You'll spend WAY more time looking up interfaces for NS* than you will getting comfortable with Objective-C, I guarantee.

    In fact, I'd say keeping the Mac in mind while developing on Windows has made me a better Windows developer. Keep the possibility of a Cocoa front end in mind, and you'll produce much cleaner code that doesn't contaminate basic functionality with platform-specific issues the way almost all Windows code written today inevitably does.

    So, you know, eat your square brackets. They're good for you.
     
  16. eastcoastsurfer macrumors 6502a

    Joined:
    Feb 15, 2007
    #16
    I would say picking up Cocoa framework really is no different. Objective-C is available on any platforms that support GCC (which means nearly every platform). Although you will most likely be missing many of the Apple libraries, GNUStep aims to provide some support.
     
  17. mduser63 macrumors 68040

    mduser63

    Joined:
    Nov 9, 2004
    Location:
    Salt Lake City, UT
    #17
    Also, you can write Cocoa apps using Ruby and Python, and Java (although admittedly the Java-Cocoa bridge is deprecated), so you do in fact have a choice. It's not just Objective-C. I agree with the other posters that you should just learn ObjC. It's not that hard if you know C and if you're like me, you'll grow to really enjoy working in it. I'd rather write in ObjC than in any other language.
     
  18. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #18
    If you use C++, it's not that hard to write a portable program. If the core application is written entirely in pure, portable C++, then it's the GUI frontend that will change. And that won't be a problem in OS X, since you can wrap it up in ObjC style...
     
  19. MichaelS thread starter macrumors newbie

    Joined:
    Dec 1, 2007
    #19
    Thanks again to all (robbieduncan, Gelfin, eastcoastsurfer, mduser63, Soulstorm) for your help.

    Now, does anybody here actually have experience writing Mac OS X apps the way outlined --- using straight C++ for the core parts and Objective-C/Cocoa to implement the user-interface --- or is this just the way you would go if you were in my situation?

    Also, what literature would be useful to me? Apple recommends "Learning Cocoa with Objective-C", 2nd Edition, by James Duncan Davidson, and "Building Cocoa Applications: A Step by Step Guide" by Simson Garfinkel and Michael Mahoney. However, both books were published in 2002, so are presumably a bit dated by now.

    Do people know "Cocoa Programming for Mac OS X", 2nd Edition, by Aaron Hillegass? It was published in May 2004, but is still said to be useful today (a 3rd edition is due out in spring 2008). Another text I found is "Programming in Objective-C" by Stephen Kochan, published in December 2003. Is this worth reading in your experience?

    Any other books you would recommend?
     
  20. Gelfin macrumors 68020

    Gelfin

    Joined:
    Sep 18, 2001
    Location:
    Denver, CO
    #20
    I'm in the scope-and-spec phase of a skunkworks project at my current employer along these lines, but I haven't got much specific advice to pass on at the moment. Most of what I'm doing right now is factoring the Windows-isms out of a legacy codebase, figuring out which parts I can jettison since OS X gives them to me for free, and charting out a proper Objective-C interface for it all.

    The only thing that's making me crazy is the same thing that's always made me crazy about this code: it uses Boost. Not a fan.

    Honestly, I've gotten by mostly with just the Apple documentation. I did actually flip through a lot of that "Learning Cocoa" book and twiddle with the examples. It's still helpful. Just be sure to search the threads here before posting the inevitable "OMG WHERE'S THE CLASS TAB" post. I swear there must have been fifty such threads started within the last month. It should probably be a sticky topic or something.

    You won't see a lot of the newer stuff like Core Data and bindings, but again, Apple documents those fairly well.

    I don't own Hillegass, but it seems to be the standard, sort of to Mac programming what Petzold is on the Windows side. It's highly recommended. I'll probably pick up a copy once the 3rd ed. is released. I honestly wouldn't recommend getting a book that's just Objective-C. If you know C, the Apple-supplied Obj-C language spec is more than enough to get you rolling. It's actually quite a bit more readable than, say, Stroustrup.
     
  21. MichaelS thread starter macrumors newbie

    Joined:
    Dec 1, 2007
    #21
    Gelfin, thanks for your comments!

    I just found out about this book "Cross-Platform Development in C++" by Syd Logan to be released later this month. The website is http://www.crossplatformbook.com/. Maybe that's also a book worth buying when it's available? At least it does the Mac OS X part using Cocoa, so some amount of Objective-C must also be covered.
     
  22. Gelfin macrumors 68020

    Gelfin

    Joined:
    Sep 18, 2001
    Location:
    Denver, CO
    #22
    Funny, I just found out about that book myself. You know, just now when you posted it. Always hard to know for sure, but it looks like it could be a fairly excellent sort of book. And the timing couldn't be better, considering how hard it is for folks to buy Christmas presents for me. ;)

    From the description, it may not cover the Cocoa front all that much, though. Mozilla doesn't use the standard Cocoa UI development model. They took a much harder approach to cross-platform UI, by developing their own toolkit that works across supported platforms. It's also why Mozilla projects don't usually look quite exactly like a normal Mac application. If you need to write something cross-platform and have access to such a general UI framework, it's definitely an option to evaluate. At this point not many people will have the frame of reference to help you decide whether it's the best option. If you find out, come back and tell us. :)
     
  23. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #23
    Might I suggest Trolltech's Qt for Mac? The latest version is 4.3.2

    I use it professionally on Linux, and have installed the Mac version at home (haven't used it much there yet).
     
  24. mduser63 macrumors 68040

    mduser63

    Joined:
    Nov 9, 2004
    Location:
    Salt Lake City, UT
    #24
    If you decide to go with ObjC and/or Cocoa, the two books you mentioned, "Programming in Objective-C" by Stephen Kochan and "Cocoa Programming for Mac OS X" by Aaron Hillegass are IMO the way to go. They're both sort of the "standard" for new Cocoa programmers. Kochan's book in particular is one of the best technical books I've ever read.
     
  25. unknown.exe macrumors member

    unknown.exe

    Joined:
    Sep 22, 2007
    Location:
    Somewhere on Earth
    #25
    Yes I definately agree, also it is a good idea to wrap C++ w/ cocoa/C. All of the above suggestions are superb. But I have recently stumbled upon a little chapter from hevean in the C++ Book for Dummies that addresses your problem down to a word. I would definately pick it up. Also, stick w/ C++... it's a better language than most others and easier to learn as an intermediate programmer.:apple:
     

Share This Page