6

I refactored some code in my iOS app and have started receiving the following error

Undefined symbols for architecture x86_64: "OBJC_CLASS$_UBApplicationStartupReasonReporterNotificationRelay", referenced from: 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)

So here is what I did..

Earlier I had all the files at root. To organise I moved all files under new folder vendor/ios-startup-reporter/{files}.{h,m}

Then created a file vendor/StartupReporter.h which contains following content

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

now, In AppDelegate When I import it like this (#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

I get the above error,

Can someone help me what I am doing wrong and how I can fix it?

Alwaysblue
  • 9,948
  • 38
  • 121
  • 210
  • This is rather about project configuration and build settings. Could you give access to broken project (branch on GitHub?)? Also which Xcode version do you use? – Asperi Apr 14 '22 at 14:02

2 Answers2

4

I've had this issue with react-native and XCode for some time on my M1 Mac (albeit a different library), generally the following does help fix the issue:

Clean Project

  1. Clean the project CMDK
  2. Clear the derived data
rm -rf ~/Library/Developer/Xcode/DerivedData
  1. Close XCode
  2. Run the following in your Pods directory:
pod cache clean --all
rm -rf Pods
pod install --repo-update
  1. (optional) Fresh npm install
  2. Restart XCode

Check Linked Libraries

If the above doesn't fix the issue make sure that there isn't any artifacts left over from before your refactoring. There may be a couple places to check for linked libraries that were renamed or don't exist anymore.

Check Targets > Build Phases > Link Binary with Libraries

Asleepace
  • 3,466
  • 2
  • 23
  • 36
  • Fresh `npm install`? – Alwaysblue Apr 12 '22 at 14:13
  • @iRohitBhatia yeah I would for good measure, make sure that the header files you changed aren’t linked anywhere in Xcode as well – Asleepace Apr 12 '22 at 14:15
  • Haha. it's a react native project but what gave it away? like how do you know it's the react-native project? Pretty sure it's not because of that though. – Alwaysblue Apr 12 '22 at 14:32
  • Haha I read the title and it gave me PTSD, you also have a react header too, but more importantly are you on an M1 mac? – Asleepace Apr 12 '22 at 14:36
  • Right. So I restructured my code base and put it inside the folders (like everything was on root though -> Stupid xcode stuff). The restructured code doesn't work but when everything is on root, it works fine. Hence pretty sure it's something to do with Xcode and headers but thanks so much for the help. – Alwaysblue Apr 12 '22 at 14:39
  • @iRohitBhatia yeah no worries, this answer may help: https://stackoverflow.com/a/53178233/4326715 – Asleepace Apr 12 '22 at 14:40
3

First of all, if the header file is in the same module as the AppDelegate, do not import it by the relevant path, but import it by the filename:

#import "vendor/StartupReporter.h"

to

#import "StartupReporter.h"

It will be found and correctly imported.

Secondly, make sure that all .m files are linked to the app target. Undefined symbols means that the class name is recognizable by the compiler, but the symbols (actual implementation) cannot be found. Usually, this happens when we create an {h,m} file and forget to add the .m file to the target membership.

enter image description here

arturdev
  • 10,884
  • 2
  • 39
  • 67