1

So far I am only able to code WatchOS apps in Objective C or Swift.

I have a few C++ code libraries I would like to use on WatchOS.

Every time I try to include them I get compile time errors such as

error: unknown type name ‘class’

int MyUtil::MyFunction(int value1_)
{
    return value1_ * 2;
}
nathancy
  • 42,661
  • 14
  • 115
  • 137
  • There are a lot of tutorials and instructions how to use a C++ Library from Swift. It's not only WatchKit related. – Marc T. May 12 '19 at 17:09

1 Answers1

3

Yes, you can include C++ code in a WatchOS app, having personally done it with an Objective C app that has a phone and watch component.

To start, you'll probably have to manually add the header search path and/or source files to your project settings (Build Phases -> Compile Sources). Likewise, you'll probably need to manually add the library to your linker settings (Build Phases -> Link Binary With Libraries).

The slightly more annoying problem is dealing with C++ name mangling issues. The way I worked around this was to create a an abstraction layer in C. In other words, my Objective C code calls functions I wrote in C, which, in turn, call the C++ library. If you take this approach, be sure not to include any of the C++ headers from your C language header file or you'll be dealing with name mangling issues again.

M. Simms
  • 61
  • 3