Search code examples
objective-cxcodelinker-errorsmach-o

Apple Mach-O Linker Error: 1 duplicate symbol for architechture


This is the error I get when I run my project in Xcode:

duplicate symbol _coinsTotal in:

/Library/Developer/Xcode/DerivedData/AppName-fqlzuwivxudvndbinqsoudxkdzrg/Build/Intermediates/AppName.build/Debug-iphonesimulator/AppName.build/Objects-normal/i386/ViewController.o

/Library/Developer/Xcode/DerivedData/AppName-fqlzuwivxudvndbinqsoudxkdzrg/Build/Intermediates/AppName.build/Debug-iphonesimulator/AppName.build/Objects-normal/i386/AppDelegate.o

ld: 1 duplicate symbol for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation)

The error occurs because I import my ViewController.h in my AppDelegate.m but I need to do this so I can add the coin totals after my reward video plays. I have added my ViewController.h to my AppDelegate.m in other apps with no errors.

Any ideas or suggestions? Thanks!

Here is my code in the ViewController.h file at the top:

#import <UIKit/UIKit.h>

#import <Chartboost/Chartboost.h>



int coinsTotal;
int pointsLeft;
int dailyTwenty;


@interface ViewController : UIViewController <UIActionSheetDelegate>

Here is the code for my AppDelegate.m file:

#import "AppDelegate.h"

#import "ViewController.h"

#import <CommonCrypto/CommonDigest.h>
#import <AdSupport/AdSupport.h>
#import <Chartboost/Chartboost.h>
#import <Chartboost/CBNewsfeed.h>

@interface AppDelegate ()<ChartboostDelegate>

@end

@implementation AppDelegate

Solution

  • You are declaring your three int values in the header file as globals. Everywhere you include this header in your code, you'll be defining/redefining them.

    You could declare them as extern in your header file:

    /* in ViewController.h */
    extern int coinsTotal;
    extern int pointsLeft;
    extern int dailyTwenty;
    

    And then declare them once at the top of your AppDelegate.m outside of the @implementation code.

    /* in AppDelegate.m */
    int coinsTotal = 0;
    int pointsLeft = 0;
    int dailyTwenty = 0;
    ...
    
    @implementation AppDelegate
    ...
    @end
    

    But I would prefer to create them as members of a singleton class and then include the interface header file where ever you need to set or read the values.