Undefined symbols for architecture i386: "_OBJC_CLASS_$_XX" 错误解决方法

以下是我在一个工程中的类似操作带来的错误,现在将其简化。

首先新建一个工程,再新建一个Target:Undefined symbols for architecture i386:

不计单元测试部分的话,现在工程中有两个Target:Demo和ATarget。

在Demo的ViewController中添加一个类方法并公开接口:

+ (void)logClassName {
    NSLog(@"%@", [self class]);
}

在ATarget的AppDelegate中调用该方法:

#import "ViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [ViewController logClassName];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

选中ATarget对应的Scheme,Command + R


报错:

Undefined symbols for architecture i386:
  "_OBJC_CLASS_$_ViewController", referenced from:
      objc-class-ref in AppDelegate.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

原因:在ATarget中找不到ViewController这个类。

解决方法:打开ATarget的Build Phases,在Compile Sources中添加ViewController.m文件Undefined symbols for architecture i386:


重新运行,无错通过。


出现Undefined symbols for architecture i386:   "_OBJC_CLASS_$_XX"的错误,原因通常在于无法识别的标记所在的类文件没有被加入到Compile Sources中。


你可能感兴趣的:(iOS,错误处理)