views:

6951

answers:

6

Hi. I would like to write applications that use C++ and the Cocoa frameworks because Apple are not making Carbon 64-bit capable. C++ seems to be pretty vanilla in it's implementation on Linux and Windows but on Mac OS X it seems like additional Apple specific pieces of code are required (like an Obj-C wrapper). It also seems that Apple are forcing developers to write in Objective-C rather than C++, I could be wrong though.

I am trying to find a path to write code on the Mac that would be easy to keep cross platform. Having to write code in C++ for Linux/Windows and then rewrite large portions in Objective-C would be very inefficient.

Is there a way to write code in C++ that will be supported for the future and supported in Xcode? Also, If this is possible how would I mix C++ and Objective-C in Xcode? Thanks.

+12  A: 

Yes, you can just use C++ (i.e. writing it in *.cpp files) and even mix C++ and Objective-C inside *.mm files (standard Objective-C code is stored in *.m files).

Of course, you still have to use Objective-C for your user-interface and create Objective-C wrappers for your C++ objects. Another option is to switch to QT which is a C++ Framework that supports Windows, Mac OS X, and Linux -- and will be released under the LGPL with the next version 4.5.

fhe
Note that if you use Qt, your app will suck. Qt-based apps do not look and feel like native Mac apps. (For an example, see Google Earth.)
Peter Hosey
Yes, this is probably a trade-off you'll have to make. It's the price you pay for the reduced development effort :-)
fhe
I think that the guy is interested in game development.
tunnuz
Peter: That's not true at all. Qt-based apps can look and feel identical to native Mac apps, you just need to do per-platform tweaking, something that is much easier than writing a native GUI on each platform.
Mike McQuaid
Mike, you are misinformed. Among their other shortcomings, Qt-based apps on the mac do not use the native controls at all, and the Qt library does all of the drawing itself. This means that Qt apps don't get any hardware acceleration for 2D rendering, they don't stay in sync with UI changes that Apple makes to the standard controls, and a Qt app can't deliver ADA compliance or scriptability unless you reinvent those wheels yourself.In other words, DO NOT ATTEMPT to ship a Qt app on the Mac. Google can get away with it: you can't.
NSResponder
+4  A: 

Yes you can mix them.

You need to use Objective-C to directly operate on your GUI objects and receive notifications from them.

These Objective-C objects can directly call C++ logic if you put them in .mm files, instead of the pure Objective-C .m files. Note that you may see older advice suggesting using an uppercase .M to indicate Objective-C++ but this is very flaky and likely to confuse you as well as the compiler.

You don't need to wrap each and every C++ object but your Objective-C code will need to contain pointers to them.

Apple have a full sample Cocoa with Carbon or CPP. with some more details in the readme.

Andy Dent
+2  A: 

This very question was the focus of an entire "Late Night Cocoa" podcast episode on the Mac Developer Network about a month ago:

LNC Episode 037: Porting Large Applications to the Mac platform

If you're a MDN member then you already have access to this podcast, otherwise it's $1.99. I listened to this one when it came out and while it wasn't terribly relevant to me (I have little interest in writing cross-platform apps in C++), you can see from the table of contents on that web page that it covers exactly the subjects you are asking about - why you can't use Carbon, how to incorporate C++ with Objective-C/Cocoa code, and using cross-platform GUI frameworks like Qt. Highly recommended.

erikprice
Wow thank you Eric. This might be exactly what I'm looking for. Thanks, ill take a look (Vote up)
Brock Woolf
Now it's also free.
Maleev
+2  A: 

If you're just looking to use plain vanilla C++, this is absolutely supported and really no different than any other platform. Xcode even has a template for it under File > New Project > Command Line Utility > C++ Tool. Also, a number of the popular open-source libraries (libcurl, libxml2, sqlite, etc) come with OS X and are available for dynamic linking. You don't have to use Cocoa or anything Apple-specific if you don't want to.

If you do want to use Cocoa in certain portions of your app, take a look at Objective-C++. You can mix C++ and Objective-C in the same file by giving it an extension of .mm, or by right-clicking on the file in Xcode and selecting Get Info > General then changing the File Type to sourcecode.cpp.objcpp. The second option is useful if you have a .cpp file where you want to use Objective-C within a Mac-specific #ifdef.

Matt Stevens
+22  A: 

You cannot write a Cocoa application entirely in C++. Cocoa relies heavily on the late binding capabilities of Objective-C for many of its core technologies such as Key-Value Bindings, delegates (Cocoa style), and the target-action pattern. The late binding requirements make it very difficult to implement the Cocoa API in a compile-time bound, typed language like C++ 1. You can, of course, write a pure C++ app that runs on OS X. It just can't use the Cocoa APIs.

So, you have two options if you want to share code between C++ apps on other platforms and your Cocoa-based application. The first is to write the model layer in C++ and the GUI in Cocoa. This is a common approach used by some very large apps, inlcuding Mathematica. Your C++ code can be left unchanged (you do not need "funky" apple extensions to write or compile C++ on OS X). Your controller layer will likely make use of Objective-C++ (perhaps the "funky" Apple extension you refer to). Objective-C++ is a superset of C++, just as Objective-C is a superset of C. In Objective-C++, you can make objc-style message passing calls (like [some-objc-object callMethod];) from within a C++ function. Conversely, you can call C++ functions from within ObjC code like:

@interface MyClass {
    MyCPPClass *cppInstance;
}
@end

@implementation MyClass
- (id)init {
    if(self = [super init]) {
        cppInstance = new MyCPPClass();
    }
    return self;
}
- (void) dealloc {
    if(cppInstance != NULL) delete cppInstance;
    [super dealloc];
}
- (void)callCpp {
    cppInstance->SomeMethod();
}
@end

You can find out more about Objective-C++ in the Objective-C language guide. The view layer can then be pure Objective-C.

The second option is to use a cross-platform C++ toolkit. The Qt toolkit might fit the bill. Cross-platform toolkits are generally despised by Mac users because they do not get all the L&F details exactly right and Mac users expect polish in the UI of Mac applications. Qt does a surprisingly good job, however, and depending on the audience and the use of your app, it may be good enough. In addition, you will loose out on some of the OS X-specific technologies such as Core Animation and some QuickTime functionality, though there are approximate replacements in the Qt API. As you point out, Carbon will not be ported to 64-bit. Since Qt is implemented on Carbon APIs, Trolltech/Nokia have had to port Qt to the Cocoa API to make it 64-bit compatible. My understanding is that the next relase of Qt (currently in release candiate) completes this transition and is 64-bit compatible on OS X. You may want to have a look at the source of Qt 4.5 if you're interested in integrating C++ and the Cocoa APIs.

1 For a while Apple made the Cocoa API available to Java, but the bridge required extensive hand-tuning and was unable to handle the more advanced technologies such as Key-Value Bindings described above. Currently dynamically typed, runtime-bound languages like Python, Ruby, etc. are the only real option for writing a Cocoa app without Objective-C (though of course these bridges use Objective-C under the hood).

Barry Wark
A: 

If you're writing a purely graphical application, i.e. you're drawing everything using code, consider openFrameworks. It's an opensource graphical programming languages built on top of C/C++. It has addons which allow people to extend the language. They have an addon for the iphone. I believe that it comes with the library and the XCode projects that will help you compile apps for the iPhone and iPod touch.

milesmeow