Apple Mach-O linker error with duplicate symbols even after linking the .m class files to the build phases in Xcode

StackOverflow https://stackoverflow.com/questions/16144666

Question

When i build and run my project the only error i get, turns out to be the Apple Mach-O linker error

20 duplicate symbols for architecture i386

Linker command failed with exit code 1 (use -v to see invocation)

The problem is that i've checked the existance of any duplicate class files, pictures, audio files in my project(weren't any). I have imported all the .m files to the build phases-> compile sources. Also have the correct frameworks imported in link with binary libraries and checked whether the picture, xib, audio files have been imported properly in the copy bundle resources and so far everything looks good to go. But it isn't, as I try to run the project, the linker error stays put. Can anyone tell me where am I going wrong?

 Ld /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Products/Debug-iphonesimulator/Taukytable.app/Taukytable normal i386
        cd "/Users/User/Desktop/Taukytable Newone"
        setenv IPHONEOS_DEPLOYMENT_TARGET 6.0
        setenv PATH "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
        /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch i386 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk -L/Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Products/Debug-iphonesimulator -F/Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Products/Debug-iphonesimulator -filelist /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/Taukytable.LinkFileList -Xlinker -objc_abi_version -Xlinker 2 -fobjc-arc -fobjc-link-runtime -Xlinker -no_implicit_dylibs -stdlib=libc++ -mios-simulator-version-min=6.0 -framework CoreAudio -framework CoreMedia -framework CoreFoundation -framework Accelerate -framework CoreData -framework CoreVideo -framework Security -framework CoreImage -framework MessageUI -framework MobileCoreServices -framework AVFoundation -framework QuartzCore -framework SystemConfiguration -framework OpenGLES -framework AssetsLibrary -lsqlite3.0 -framework AudioToolbox -framework UIKit -framework Foundation -framework CoreGraphics -o /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Products/Debug-iphonesimulator/Taukytable.app/Taukytable
    duplicate symbol _SavedImage in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/MasterViewController.o
    duplicate symbol _SavedImage1 in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/MasterViewController.o
    duplicate symbol _vvvalue in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/MasterViewController.o
    duplicate symbol _lll in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/MasterViewController.o
    duplicate symbol _vvvalue in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/PAsavee.o
    duplicate symbol _SavedImage in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/PAsavee.o
    duplicate symbol _lll in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/PAsavee.o
    duplicate symbol _SavedImage1 in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/PAsavee.o
    duplicate symbol _SavedImage in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/Preview.o
    duplicate symbol _SavedImage1 in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/Preview.o
    duplicate symbol _vvvalue in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/Preview.o
    duplicate symbol _lll in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/Preview.o
    duplicate symbol _SavedImage in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/NSObject+ShareClass.o
    duplicate symbol _SavedImage1 in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/NSObject+ShareClass.o
    duplicate symbol _vvvalue in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/NSObject+ShareClass.o
    duplicate symbol _lll in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/NSObject+ShareClass.o
    duplicate symbol _vvvalue in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/CustomOverlayView.o
    duplicate symbol _SavedImage in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/CustomOverlayView.o
    duplicate symbol _SavedImage1 in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/CustomOverlayView.o
    duplicate symbol _lll in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/CustomOverlayView.o
    ld: 20 duplicate symbols for architecture i386
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
Was it helpful?

Solution

This is really really basic beginner stuff that suggests to me you don't understand the difference between C-family language's "header files" and "implementation files".

Did you search your project and not notice that you are declaring variables in header files but without extern? You even had the name of the global variable in your linker error. Yes, global variables are emitted as symbols into object files (.o) and if you include a header into three files and all three files then declare the same global variable, you get this error. It was for just this issue that the mighty and wonderful extern keyword was invented.

In the context of header files you should not declare actual variables, only extern references to them. This is something you should learn on your first day of C, C++ or Objective C programming. You can however put code into header files, but then, every file you include that header into will generate a duplicate set of symbols, leading to link errors, and further accelerating the pace of global warming, and causing the melting of icebergs. (Just kidding about that last part.)

Here is your mistake, this is a header file but you are treating it like it was an implementation file and declaring not external-variable-declarations but actual-emit-the-global-variable-into-the-object-file declarations:

#import <Foundation/Foundation.h>

UIImage *SavedImage;
UIImage *SavedImage1;

NSString * vvvalue;
//UILabel * l2;
NSString * lll;

@interface NSObject (ShareClass)

@end

You tell me now. Where should the keyword extern appear in the code sample above? Where does the real "emit this variable into object file" declaration of a variable go? In a header file? NO. In the implementation file (.m or .c or .cpp as the case may be).

Secondly, after you add the extern keyword, you now have a second problem. You probably need to add the real declaration of that global variable in your corresponding .m file. Yes, that's right you have to "declare" every global twice in a C-family language program. once as extern in header files if you really need all the modules in your system to be able to see that global variable, and once without the extern keyword, in only one of your .m files.

Of course, you do know that such "global variable mess" is to be avoided, and is terrible programming practice right? You should be thinking about keeping global variables out of your code, and have variables that limit their scope to a single implementation module wherever possible.

In .h file:

extern UIImage * myImage;

In .m file:

UIImage *myImage;

Go read your book on what the extern keyword means please. Also, I suspect that you might have meant to do this:

@interface NSObject (ShareClass)
  UIImage * InstanceVariable;  
@end

The above is an instance variable (belongs to each instance of the class). It is common if you need to group some stuff and put it inside an object not to make it a global variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top