33

I've been learning Objective-C and Cocoa by working my way through the Hillegass book and it occurs to me that I might be better off using Objective-C++. Objective-C seems like the clear choice for developing UIs but I have a very strong C++ background and would love to develop application back-ends in C++ and use Objective-C++ to do the UI integration. But I wonder if Apple will keep developing Objective-C++ or will it become a dead end.

Is anyone out there using Objective-C++?

7 Answers 7

29

Disclaimer: I don't work or speak for Apple, so this is my opinion:

I can't speak for the major dev shops, but in my small group, we've used Objective-C++ both for integrating C++ libraries, and as you propose for writing backends in C++. As @alxp mentions, things like exception handling across the language boundary are painful, but with a little planning, most of these pains can be avoided. For experienced C++ devs, the gains can be well worth the pain.

In terms of support, I think you can assume that support in its current state won't go away any time soon. It's part of the GCC code base and the Clang toolchain (Apple's next compiler toolchain) fully supports Objective-C++. On the other hand, there isn't any official guarantee that Apple will continue to develop the integration—fixing some of the warts, for example.

For current projects, I would say that if using Objective-C++ provides benefit, it is safe to rely on the existing support and you should use it.

5
  • 6
    I recently used Objective-C++ in my app and I found it to be very profitable. Another example of a wart: openradar.me/6411137 Fret not about the support of Objective-C++: trac.webkit.org/browser/trunk/WebKit/mac/Storage/… There is a lot of C++ code around and it is much more profitable to maintain Objective-C++ at a working level than to rewrite large amounts of code. Jan 5, 2011 at 5:29
  • What do you think of the state of Objective-C++ now, a few years later? Jun 5, 2011 at 6:34
  • 3
    @Garet, the Clang toolchain has excellent Objective-C++ support. I'd say my assessment above still stands.
    – Barry Wark
    Jun 6, 2011 at 15:41
  • 5
    The Objective-C runtime is partially written in Objective-C++. It's probably quite safe to rely on it.
    – rob mayoff
    Jan 31, 2012 at 4:06
  • Several of Apple's frameworks are written in C++ and Objective-C++. Just take a look at some of the stack traces in AppKit and related frameworks. This fact is also mentioned by Apple engineers e.g. in various WWDC talks. It's probably a safe bet that Obj-C++ won't go away any time soon - if at all - as long as OS X (and iOS) are alive and kicking.
    – Jay
    Apr 17, 2014 at 17:56
7

The only times I've used ObjC++ was to port libraries to make them accessible from my ObjC code. The clashes between how ObjC++ and ObjC handle things like exceptions and class creation and destruction just made it too much of a headache to juggle the two languages in one project.

I don't think support will go away soon as happened with Cocoa / Java since it is pretty solidly part of GCC, and the fact that Objective-C++ compiles down to straight C++ in the same way that Objective-C can compile down to straight C, but I still don't find it a very pleasant environment to build software in compared with Objective-C and being able to comfortably fully use the OS X-provided frameworks.

4

Objective-C++ is likely to remain supported as long as Objective-C is. Obj-C++ is a basic goal for clang, which is expected to eventually replace gcc as Apple’s preferred compiler. Usage is likely to rise as Carbon applications are moved to Cocoa front ends.

Of course, the word “likely” appears twice above because Apple is so excitingly unpredictable. :-)

2

I suspect Apple will continue to support Objective C++ for a while, as I don't see any significant recurring effort required by Apple to maintain Objective C++ as Apple updates Cocoa and Objective C++.

1

The other day I was surprised when I attempted to refactor some Objective-C code that was within an Objective-C++ file using Xcode's refactoring support. Even though the menu items are enabled I got the "Can’t refactor Objective-C++ code. Xcode can only refactor C and Objective-C code." error message. So while compilation of Objective-C++ will continue to function indefinitely I suspect Objective-C++ will be a second class citizen within Xcode.

0

I took it out of all my code. No .mm files.

But you need .cpp files to talk to .m files. The solution is a .c/.h file that keeps the blood brain barrier intact.

C++ with objective-C is usually just too much baggage.

-1

Objective-C is really just a bunch of C code that emulates objects in the C language, and it still uses the C compiler including the Objective-C header files. Using Objective-C++ uses the C++ compiler and include the Objective-C headers as C code, since C++ will run C code. Objective-C++ is essentially just C, C++, and Objective-C (which is really just C)

2
  • 1
    I could be wrong, but I don't think you can compile Objective-C as regular C code.
    – user764357
    Jul 3, 2013 at 3:20
  • why is there no such thing as a objective c compiler then. I use the gcc compiler for c and objective-c so long as I include the objective C headers, and g++ for c++ and objective c++ Jul 10, 2013 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.