c++ - 将 Objective C++ 用于 WatchKit

标签 c++ ios xcode objective-c++ watchkit

当我尝试将 Objective C++ 用于我的 WatchKit 扩展时,我收到链接器错误消息,如

Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_WKInterfaceController", referenced from:

我尝试使用单 View 应用程序模板创建一个新项目,然后向其中添加一个简单的 WatchKit App 目标。在不做任何更改的情况下,整个编译过程都很好,但是如果我将 InterfaceController.m 文件重命名为 InterfaceController.mm,那么即使是那个简单的项目也会出现上述错误。

如何使用 Objective C++ 构建 WatchKit 应用扩展?

最佳答案

我在网上或 Apple 的开发论坛上找不到任何关于此的内容,但我最终弄清楚了发生了什么并想出了一个解决方法。

问题似乎是,如果所有源文件都在 C++ 和 Objective C++ 中,则链接器的某种神奇触发器不会发生。解决方案是创建一个包含 WatchKit header 的 .m 文件。该文件的内容只需要这个:

#import <WatchKit/WatchKit.h>

将其保存到 .m 文件中,将其添加到项目中,一切都会变得阳光和彩虹。

我通过 Radar 向 Apple 报告了这件事并得到了这样的回复:

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.

关于c++ - 将 Objective C++ 用于 WatchKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29043740/

相关文章:

c++ - 更快的导出嵌入数据的方法

ios - 在像 GIF 一样循环播放的 UIView 中自动播放 MP4 视频?

iPhone 的 iOS 模拟器版本未显示错误的 console.log 输出

Javascript/AngularJS 要求消费者下载 iOS/Android 应用程序

swift - 如何在带有标签和按钮的页面的一半上显示 WKWebView

ios - Xcode - 标题为 "no such file or directory"

c++ - atomic<T*> 总是无锁的吗?

c++ - 在模板函数参数之间强制执行所需的关系

c++ - std::condition_variable::wait_until 相对于 std::this_thread::sleep_for 有什么优势吗?

php - 要显示的设备特定内容