# Objective C++ option for source files

**URL:** <https://forum.juce.com/t/objective-c-option-for-source-files/24003>\
**Category:** The Projucer\
**Created:** [September 1, 2017, 9:12am UTC](https://forum.juce.com/t/objective-c-option-for-source-files/24003 "2017-09-01T09:12:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![siedschl](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@siedschl](https://forum.juce.com/u/siedschl)\
**Post date:** [September 1, 2017, 9:12am UTC](https://forum.juce.com/t/objective-c-option-for-source-files/24003/1 "2017-09-01T09:12:50Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [September 1, 2017, 9:16am UTC](https://forum.juce.com/t/objective-c-option-for-source-files/24003/2 "2017-09-01T09:16:31Z")

</div>

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

Rail

---

<div class="post-metadata">

**Author:** ![siedschl](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@siedschl](https://forum.juce.com/u/siedschl)\
**Post date:** [September 1, 2017, 9:18am UTC](https://forum.juce.com/t/objective-c-option-for-source-files/24003/3 "2017-09-01T09:18:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [September 1, 2017, 9:18am UTC](https://forum.juce.com/t/objective-c-option-for-source-files/24003/4 "2017-09-01T09:18:48Z")

</div>

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

Rail

---

<div class="post-metadata">

**Author:** ![siedschl](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@siedschl](https://forum.juce.com/u/siedschl)\
**Post date:** [September 1, 2017, 9:22am UTC](https://forum.juce.com/t/objective-c-option-for-source-files/24003/5 "2017-09-01T09:22:32Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [September 1, 2017, 9:32am UTC](https://forum.juce.com/t/objective-c-option-for-source-files/24003/6 "2017-09-01T09:32:07Z")

</div>

Check out how the JUCE modules include their .mm files

Rail

---

<div class="post-metadata">

**Author:** ![siedschl](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@siedschl](https://forum.juce.com/u/siedschl)\
**Post date:** [September 1, 2017, 9:53am UTC](https://forum.juce.com/t/objective-c-option-for-source-files/24003/7 "2017-09-01T09:53:42Z")

</div>

So I’m guessing the answer is no… 🙂

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [September 6, 2017, 12:01pm UTC](https://forum.juce.com/t/objective-c-option-for-source-files/24003/8 "2017-09-06T12:01:40Z")

</div>

> [@siedschl](#):
>
> So I’m guessing the answer is no…

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 ...
#endif

```

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

---

<div class="post-metadata">

**Author:** ![siedschl](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@siedschl](https://forum.juce.com/u/siedschl)\
**Post date:** [September 6, 2017, 2:54pm UTC](https://forum.juce.com/t/objective-c-option-for-source-files/24003/9 "2017-09-06T14:54:21Z")

</div>

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.
