0

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
LodgeApps
  • 344
  • 1
  • 2
  • 21
  • You should post your code as this sounds like it's a duplicate import error. Is your view controller the root ire controller of the app delegate? If so then you should be able to get to it that way by saying window.rootViewController and casting it as your view controller – bolnad Jul 16 '15 at 02:32
  • How is `coinsTotal` declared in your ViewController.h file? – Phillip Mills Jul 30 '15 at 16:08
  • @PhillipMills it is *int coinsTotal; – LodgeApps Jul 30 '15 at 17:14
  • half the time I get this error when autocomplete imports a .m file instead of a .h How are you importing the viewController into the app delegate? – Alex Reynolds Jul 30 '15 at 17:27
  • @AlexReynolds I am not importing a .m file, I'll post my beginning code for the delegate.m – LodgeApps Jul 30 '15 at 17:41
  • Why are those variables not instance variables? Why do you declare them outside the class? – Alex Reynolds Jul 30 '15 at 17:43
  • @AlexReynolds That is just how I learned how to do it, would making instance variable fix the issue? – LodgeApps Jul 30 '15 at 17:45
  • Yes. Or atleast I'm pretty sure. Importing the h into appdelegate is probably redefining the variables when compiled. – Alex Reynolds Jul 30 '15 at 17:46
  • @AlexReynolds This solved the importing viewcontroller.h issue but in my delegate.m it did not recognize the variables – LodgeApps Jul 30 '15 at 17:58
  • Ya it wouldn't recognize them unless accessed through the class. If want global variables, which is not good in my opinion but you just use the `extern` keyword. Glad you got it worked out – Alex Reynolds Jul 30 '15 at 18:51

3 Answers3

4

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.

Dan Loughney
  • 4,647
  • 3
  • 25
  • 40
  • I tried this but got this error "_dailyTwenty", referenced from: -[ViewController didCompleteRewardedVideo:] in ViewController.o -[ViewController viewDidLoad] in ViewController.o – LodgeApps Jul 30 '15 at 18:00
  • What's the rest of the message? Did you declare int dailyTwenty; in your appDelegate.m file? – Dan Loughney Jul 30 '15 at 18:02
1

Form your code, the reason for duplicate symbols is you have declared three global variables in your "ViewController.h":

int coinsTotal;
int pointsLeft;
int dailyTwenty;

The solve this problem, declare them as "extern" in the .h file, and then declare them in the .m file:

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

/*in ViewController.m*/
int coinsTotal = 0;
int pointsLeft = 0;
int dailyTwenty = 0;

Basically, there are three reasons for duplicate symbols:

  • include a .m file instead of a .h
  • declares global variables or symbols in a .h file and include it somewhere else
  • import cycle. e.g. A imports B, B imports C, and C imports A...
Mindy
  • 429
  • 4
  • 6
-1

In my case, Importing .m instead of .h caused duplicate symbols.

Arnaud
  • 7,259
  • 10
  • 50
  • 71
Jalal Khan
  • 19
  • 1
  • 2