When I try to use Objective C++ for my WatchKit Extension, I get linker errors with messages like
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_WKInterfaceController", referenced from:
I tried creating a new project using the Single View Application template and then added a simple WatchKit App target to that. Without changing anything, the whole thing compiles fine but if I rename the InterfaceController.m file to InterfaceController.mm, then even that simple project gives the above error.
How can I build a WatchKit App Extension using Objective C++?
Best How To :
I couldn't find anything on the web or in Apple's dev forums on this, but I finally worked out what's going on and came up with a workaround.
The problem seems to be that some sort of magic trigger for the linker isn't happening if ALL of the source files are in C++ and Objective C++. The solution was to create a single .m file which includes the WatchKit header. This file only needs this for its contents:
Save that into a .m file, add it to the project and everything will be sunshine and rainbows.
I reported this to Apple through Radar and got this response:
This issue behaves as intended based on the following:
The difference between the objc and objc++ compiles comes down to objc enabling -fmodules and objc++ not. If you build the objc without -fmodules you get the same link error, and the driver is dropping -fmodules when in C++ mode, leading to the error.
Looks like we get a number of linker option load commands in the object when we use -fmodules, but we don't get them without. Thus, an object with -fmodules can find all of the frameworks it needs on its own, but an object without cannot.
So the problem is simply that objective c++ modules aren't supported, so the project has gone from using modules to not using modules. This means autolinking isn't available, so the project needs to explicitly link against the frameworks that it uses.