符号未定义,适用于x86_64架构: "_OBJC_CLASS_$_XYX"。

6

我在我的iOS应用程序中重构了一些代码,现在开始收到以下错误:

未定义符号的 x86_64 架构:
"OBJC_CLASS$_UBApplicationStartupReasonReporterNotificationRelay",
引用自: objc-class-ref in AppDelegate.o ld: symbol(s) not found for architecture x86_64 clang:
error: linker command failed with exit code 1 (use -v to see invocation)

以下是我的操作过程:

早期所有文件都在根目录下。为了整理起见,我将所有文件移动到 new 文件夹下的 vendor/ios-startup-reporter/{files}.{h,m} ,然后创建了一个名为 vendor/StartupReporter.h 的文件,其中包含以下内容。

#import "ios-startup-reporter/UBApplicationStartupReasonReporterNotificationRelay.h"
#import "ios-startup-reporter/UBApplicationStartupReasonReporter.h"
#import "ios-startup-reporter/UBApplicationStartupReasonReporterPriorRunInfo.h"
#import "ios-startup-reporter/UBApplicationStartupReasonReporterPriorRunInfoProtocol.h"

现在,在AppDelegate中,当我像这样导入它(#import "vendor/StartupReporter.h")

#import <React/RCTBridgeDelegate.h>
#import <UIKit/UIKit.h>
#import <UserNotifications/UNUserNotificationCenter.h>
#import "vendor/StartupReporter.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate>

@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) UBApplicationStartupReasonReporterNotificationRelay *notificationRelay;

@end

我遇到了上述错误,

请问有人能帮助我找出错误原因并提供修复方法吗?


这更多是关于项目配置和构建设置的问题。你能给我访问已损坏的项目(在GitHub上的分支)吗?另外,你使用的Xcode版本是哪个? - Asperi
2个回答

5

我在使用react-native和XCode时一直遇到这个问题(尽管是不同的库),通常以下步骤可以帮助解决问题:

清理项目

  1. 清理项目 CMDK
  2. 清除派生数据
rm -rf ~/Library/Developer/Xcode/DerivedData
  1. 关闭 XCode
  2. 在你的 Pods 目录下运行以下命令:
pod cache clean --all
rm -rf Pods
pod install --repo-update
  1. (可选) 进行新的npm安装
  2. 重新启动XCode

检查链接库

如果以上方法没有解决问题,请确保在重构之前没有任何残留物。可能有几个地方需要检查以查找已重命名或不再存在的链接库。

检查Targets > Build Phases > Link Binary with Libraries


新的 npm install - Alwaysblue
@iRohitBhatia 是的,为了保险起见,确保你更改的头文件在 Xcode 中没有链接到任何地方。 - Asleepace
哈哈,这是一个React Native项目,但是你怎么知道它是React Native项目呢?虽然我很确定不是因为这个。 - Alwaysblue
哈哈,我看到标题就有了创伤后应激障碍,你也有一个React头部,但更重要的是你在使用M1 Mac吗? - Asleepace
好的。所以我重新组织了我的代码库,并将其放入文件夹中(尽管一切都在根目录下 -> Xcode的愚蠢问题)。重新组织的代码无法工作,但当所有内容都在根目录上时,它可以正常工作。因此,我非常确定这与Xcode和头文件有关,但非常感谢您的帮助。 - Alwaysblue
@iRohitBhatia 没问题,这个答案可能会有所帮助:https://dev59.com/RK7la4cB1Zd3GeqPd34n#53178233 - Asleepace

3

首先,如果头文件与AppDelegate在同一模块中,请不要通过相关路径导入它,而应该通过文件名导入:

#import "vendor/StartupReporter.h"

为了

#import "StartupReporter.h"

它将被找到并正确导入。
其次,请确保所有的.m文件都链接到应用程序目标。 "Undefined symbols"意味着编译器可以识别类名,但无法找到符号(实际实现)。通常情况下,这种情况发生在我们创建了一个{h,m}文件,但忘记将.m文件添加到目标成员资格中。
请参考下图: enter image description here

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接