1. 程式人生 > 實用技巧 >Xcode 報錯:duplicate symbols for architecture x86_64錯誤分析及解決

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 編譯標示。