Xcode 报错:duplicate symbols for architecture x86_64错误分析及解决

1、参与编译的.m文件重复导入。一般是手动往工程中导入源文件时导入在了不同的目录。

解决方法也很简单,在 Target -> build parses -> complie sources,去掉重复的文件即可。

2、导入头文件时,误写为导入.m 文件

#import xxx.h 写成了 #import xxx.m

解决方法就是,导入头文件。

3、定义了重复的枚举值

这时会导致所有引用了该枚举值的都会报重复编译
笔者曾为了验证原始枚举定义和宏定义枚举时遇到该错误

//旧的枚举定义
enum NetConnectState {
    NetConnectStateDisconnected,
    NetConnectStateConnecting,
    NetConnectStateConnected,
};
//使用 每次都要写enum
enum NetConnectState state1 = NetConnectStateConnected;
//NetConnectState state = NetConnectStateConnecting;//会报错

//如果用了 typedef 重新定义枚举类型。 就可以省略 ennum,
typedef enum NetConnectState  NetConnectState;
NetConnectState state2 = NetConnectStateConnecting;

//NS_ENUM, NS_OPTION 是苹果处理过的宏,可以向后兼容,推荐使用

//枚举宏 用foundation框架提供的 枚举宏定义一个
typedef NS_ENUM(NSInteger, WJWNetStatus) {
    WJWNetStatusDisconnected,
    WJWNetStatusConnecting,
    WJWNetStatusConnected,
};

//选项宏, 多个值可以同时作为选项.
typedef NS_OPTIONS(NSInteger, WJWOrentationStatus) {
    WJWOrentationStatusTop = 1 << 0,
    WJWOrentationStatusLeft = 1 << 1,
    WJWOrentationStatusBottom = 1 << 2,
    WJWOrentationStatusRight = 1 << 3,
};
由于用了typedef 声明了同名的枚举变量,会发生枚举值重复定义
//使用 每次都要写enum
enum NetConnectState state1 = NetConnectStateConnected;
//NetConnectState state = NetConnectStateConnecting;//会报错

//如果用了 typedef 重新定义枚举类型。 就可以省略 ennum,
typedef enum NetConnectState  NetConnectState;
NetConnectState state2 = NetConnectStateConnecting;

解决方法:
尽量用宏定义枚举,只定义一次。

4、在继承协议的时候,Build Settings →Other Linker Flags 中添加了 -ObjC

协议文件不要添加 -Objc 编译标示。

原文地址:https://www.cnblogs.com/wjw-blog/p/13366249.html