Objective C++ option for source files

I’m not sure whether this is really a good and/or easily feasible idea, and of course it contradicts JUCE’s cross-platform philosophy, but: I’m sure I’m not the only one who has added small bits and pieces of Objective-C-code doing OSX-specific tasks that are not supported in JUCE. Would it be possible to have an option in Projucer telling XCode that one or more .cpp files are supposed to be compiled as Objective-C++ files? The way it is now, I have to reset that option in XCode manually after each Projucer save…

Use the extension .mm and Xcode should know it’s Obj-C++


Yes, but then it will not work in Visual Studio, right?

Surely your Objective-C++ code is MacOS only code?


I usually have one .cpp file with 99 percent cross-platform code, but just a few lines which are #ifdef’ed to do OS-specific stuff…

Check out how the JUCE modules include their .mm files


So I’m guessing the answer is no… :slight_smile:

No the answer is not no. The way to go is to put all your code into a .cpp file and then create an .mm file with the same name. At the top of the .mm file, simply include your .cpp file:

#include "MyCoolClass.cpp"

Add both MyCoolClass.mm and MyCoolClass.cpp to your .jucer project. The .mm files will be ignored in non Xcode projects, so if you’ve #ifdef-ed out the objective-c in your .cpp file it will work in all non-Xcode exporters.

In Xcode, you must also stop your code from being compiled twice (as both your .cpp and .mm file are included in your project). To do this, start your .mm file with the following:

#include "../JuceLibraryCode/JuceHeader.h" // needed for JUCE_MAC and JUCE_IOS

#if (! (JUCE_IOS || JUCE_MAC) || defined(__OBJC__))
... put all your code here ...

That’s it. You can now use Objective-C++ in your .cpp file.


Thanks, I was confused about the last point (double compilation). In the meantime, I have structured all my Obj-C++ code into helper functions, put them in a separate .mm file and declared them in the .cpp file (#ifdef’ed for MAC). Works fine too.