Creating a User Module with Obj-C++ .mm files

Hi all,

I’ve built a class with a header file and a Obj-C++ .mm file. Like this:

Foo.h

#pragma once

#ifdef __APPLE__

class Foo 
{
public:

Foo(void* windowHandle);

bar();

private:

void* handle;

};

#endif

Foo.mm

#include "Foo.h"

#import <Appkit/Appkit.h>

Foo::Foo(void* windowHandle) : handle(windowHandle) {}

Foo::bar() {
// do some stuff with NSWindow like
    NSView* nsView = (NSView*)windowHandle;
    NSWindow* nsWindow = [nsView window];
    NSAppearance* appearance = [NSAppearance appearanceNamed:NSAppearanceNameVibrantLight];
    [nsWindow setAppearance:appearance];
// ...
}

This is working fine and everything is linking correctly. However, as soon as I’ve trying to put this in a user module, I got some linking errors.

When leaving #import "Appkit/Appkit.h", Xcode is telling me Expected unqualified-id for NSObjCRuntime.h
NSZone.h
NSObject.h

And a lot of Unknown type name 'NSSString'
When I comment out #import "Appkit/Appkit.h", Xcode is complaining not knowing NSView, NSWindow and so on…

So what am I missing when creating a user module including .mm files and using Appkit symbols?
I’m sure it’s a stupid one …

Thanks,
Stefan

Do you have 2 top-level source files for that module? For instance, juce_core has both juce_core.cpp and juce_core.mm.

:weary:, thanks McMartin, as suspected this was a really stupid one from me.

By habit, I created a .cpp top level file (including Foo.mm). Changed that to .mm and now everything is compiling!

Thank you so much!!

Does the module also contain C++ source code? If so, then you should also have a top-level .cpp file and your .mm file can include the .cpp

In this case I only have one .mm file in my module. But good to know with the additional .cpp include for the future.

Thanks!