"Reference to 'Protocol' is ambiguous" when including AppKit.h
17:25 01 Jan 2026

When I turn on the Undefined Behavior Sanitizer for my scheme, I get a bunch of "Reference to 'Protocol' is ambiguous" errors in NS*.h headers. I have a .mm file that looks like this:

// myfile.mm

#include   // line 3
#include 
#include 
#include 
...

All of the errors branch out from "In file included from..." pointing to this one .mm file, and the line including AppKit.h:

In file included from /.../myfile.mm:3:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/System/Library/Frameworks/AppKit.framework/Headers/AppKit.h:10:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:6:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CoreFoundation.h:76:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFPropertyList.h:18:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFStream.h:20:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/usr/include/dispatch/dispatch.h:63:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/usr/include/os/object.h:114:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/usr/include/objc/NSObject.h:32:29: error: reference to 'Protocol' is ambiguous
   32 | - (BOOL)conformsToProtocol:(Protocol *)aProtocol;
      |                             ^

In file included from /.../myfile.mm:3:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/System/Library/Frameworks/AppKit.framework/Headers/AppKit.h:10:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:6:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CoreFoundation.h:76:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFPropertyList.h:18:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFStream.h:20:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/usr/include/dispatch/dispatch.h:63:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/usr/include/os/object.h:114:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/usr/include/objc/NSObject.h:10:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/usr/include/objc/objc.h:51:8: note: candidate found by name lookup is 'Protocol'
   51 | @class Protocol;
      |        ^
note: candidate found by name lookup is 'Protocol'
note: candidate found by name lookup is 'Protocol'

In file included from /.../myfile.mm:3:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/System/Library/Frameworks/AppKit.framework/Headers/AppKit.h:10:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:9:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:627:50: error: reference to 'Protocol' is ambiguous
  627 | FOUNDATION_EXPORT NSString *NSStringFromProtocol(Protocol *proto) API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
      |                                                  ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:616:18: note: candidate found by name lookup is 'Protocol'
  616 | @class NSString, Protocol;
      |                  ^

In file included from /.../myfile.mm:3:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/System/Library/Frameworks/AppKit.framework/Headers/AppKit.h:10:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:103:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSXPCConnection.h:179:44: error: reference to 'Protocol' is ambiguous
  179 | + (NSXPCInterface *)interfaceWithProtocol:(Protocol *)protocol;
      |                                            ^
      
etc.

All I'm doing that I can see is including AppKit.h. I don't see what else I can do to manage inclusion.

I'm missing something obvious, I expect, but I don't know what it is.

xcode include appkit objective-c++