10

I was trying to make a simple Mac Objective-C application with Xcode to keep score of two players playing a simple game with up to 36 scores per player. It isn't a very practical application because of its limited features, and it's mostly for practice. I was trying to expand the application a bit with a Preferences window, which would pop up when a menu item was clicked.

I created a file to control the men item, then a nib to pop up when it's clicked. All of this worked fine, and a new window would pop up. I put sliders, text fields, etc. on the nib, and connected them to actions. All of that worked fine.

The problem came when I tried to import the files into my root controller so that I could use the user's choices in the application.

I got the following compiler error:

Command /Developer/usr/bin/clang failed with exit code 1

Along with all of this:

Ld "/Users/myusername/Library/Developer/Xcode/DerivedData/SimpleScoreKeeper_Mac-bjvjeiybvmwfjpfilvnpezarwkml/Build/Products/Debug/SimpleScoreKeeper Mac.app/Contents/MacOS/SimpleScoreKeeper Mac" normal x86_64 cd "/Users/myusername/Dropbox/iphone app/SimpleScoreKeeper Mac" setenv MACOSX_DEPLOYMENT_TARGET 10.6 /Developer/usr/bin/clang -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -L/Users/myusername/Library/Developer/Xcode/DerivedData/SimpleScoreKeeper_Mac-bjvjeiybvmwfjpfilvnpezarwkml/Build/Products/Debug -F/Users/myusername/Library/Developer/Xcode/DerivedData/SimpleScoreKeeper_Mac-bjvjeiybvmwfjpfilvnpezarwkml/Build/Products/Debug -filelist "/Users/myusername/Library/Developer/Xcode/DerivedData/SimpleScoreKeeper_Mac-bjvjeiybvmwfjpfilvnpezarwkml/Build/Intermediates/SimpleScoreKeeper Mac.build/Debug/SimpleScoreKeeper Mac.build/Objects-normal/x86_64/SimpleScoreKeeper Mac.LinkFileList" -mmacosx-version-min=10.6 -framework Cocoa -o "/Users/myusername/Library/Developer/Xcode/DerivedData/SimpleScoreKeeper_Mac-bjvjeiybvmwfjpfilvnpezarwkml/Build/Products/Debug/SimpleScoreKeeper Mac.app/Contents/MacOS/SimpleScoreKeeper Mac"

ld: duplicate symbol _addScores in /Users/myusername/Library/Developer/Xcode/DerivedData/SimpleScoreKeeper_Mac-bjvjeiybvmwfjpfilvnpezarwkml/Build/Intermediates/SimpleScoreKeeper Mac.build/Debug/SimpleScoreKeeper Mac.build/Objects-normal/x86_64/Prefrences.o and /Users/myusername/Library/Developer/Xcode/DerivedData/SimpleScoreKeeper_Mac-bjvjeiybvmwfjpfilvnpezarwkml/Build/Intermediates/SimpleScoreKeeper Mac.build/Debug/SimpleScoreKeeper Mac.build/Objects-normal/x86_64/RootController.o for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) Command /Developer/usr/bin/clang failed with exit code 1

The (possibly) related files in my project follow.

RootController.h - All the interface declarations for stuff in the MainMenu.xib window
RootController.m - Where I need to import the files to
MainMenu.xib - The nib owned by the RootController class
Preferences.h - A file I'd want to import, but it won't work.
Preferences.m - A file I'd (maybe) want to import, but it won't work.
Preferences.xib - The nib owned by the Preferences class.
PreferencesMenuController.h - Where I declare the clickPreferences action. (Liked to MainMenu.xib)
PreferencesMenuController.m - Where I say that clickPreferences opens up Preferences nib.  (Linked to MainMenu.xib)

Is there a reason why I'd be getting this error? Is there something I need to do in the class I'm importing? Please be pretty detailed, I'm new to the language somight not know how to do certain things. And if there's anything I need to clarify, let me know.

EDIT: Here's the code to the file I can't import.

#import "Preferences.h"

@implementation Preferences

int addScores;

- (IBAction)addScoresToggled
{
    NSLog(@"addScores was toggled.");
}


- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {

    }

    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)windowDidLoad
{
    [super windowDidLoad];
}

@end

14 Answers 14

17

You can also get this error of you accidentally include the implementation file rather than the header file. e.g. #import "MyClass.m" instead of #import "MyClass.h"

Sign up to request clarification or add additional context in comments.

Comments

9

This is the reason ld: duplicate symbol _addScore

In your project you have _addScore file more than one time. check your project hierarchy.

3 Comments

I have nothing with an underscore, what exactly does that mean? Heres the code to the .m file I can't import: Edit: Since there are no line breaks in the comments, I'm going to also post the code at the bottom of the question.
#import "Preferences.h" @implementation Preferences int addScores; - (IBAction)addScoresToggled { NSLog(@"addScores was toggled."); } - (id)initWithWindow:(NSWindow *)window { self = [super initWithWindow:window]; if (self) { } return self; } - (void)dealloc { [super dealloc]; } - (void)windowDidLoad { [super windowDidLoad]; } @end
Probably late, but the "int addScore" is in static scope. So, if it's included elsewhere (or file included multiple times in the project), you'll end up with a duplicate.
3

After receiving the exact same error, I noticed that I somehow had gotten two .h files with the same name in my project. Deleting the duplicate from the project's folder (not just removing the reference) solved the issue for me.

2 Comments

Similar thing. I had two .h file reference in the project. So I deleted one reference.
Got same error .After searching the last imported files found duplicate items .
2

I ran into this issue just now because I had accidentally imported the .m file for a class instead of the .h file.

For anyone trying this answer: If the issue has appeared suddenly, have a think about what #import lines you added recently (or better yet, run a grep in git!).

Comments

1

select demo.xcodeproj,show the package content,delete the file named project.xcworkspace and the f xcuserdata

Comments

0

I recently came across this error. Are you importing the same .h file from two different files? This caused this error for me.

1 Comment

that in itself should not be a problem, sounds more like something wrong with the header file you were including (e.g. instantiating variables in a header file)
0

If you want the variable addScore to be accessable in multiple files, you need to define it in one .m file as:

int addScore;

and declare it in the associated .h file as:

extern int addScore;

If left out the "extern" keyword in the declaration, then the compiler sees that as a redefinition of addScore in every file the .h is imported/included into. That would lead to the error you are seeing.

Comments

0

I had the same problem after running two projects with the same application identifiers.

After removing (replace <yourlogin> with your account name):

project.xcworkspace/xcuserdata/<yourlogin>.xcuserdatad/UserInterfaceState.xcuserstate

it's started running under simulator but still not on the device, so I closed all opened projects, created a new one and copied files there, and finally it worked!

Comments

0

I was facing the same error when using RestKit. I selected RestKit target and cleaned/built it. then I selected my main target (my application) and cleaned/built it. that fixed it for me.

Comments

0

I meet this kind of error when I archive a project under Xcode 4.3 and 4.4, and finally I ended up this error with switching from Standard(32/64-bit Intel) to 64-bitIntel

Comments

0

I had a different cause: using Xcode 4.4 the deployment target was set to 10.4 when the the minimum was 10.6. That solved for me.

Comments

0

I ran into this error when I accidentally clicked Product -> Test, instead of Product -> Run. I just cleaned the project by clicking Product -> Clean and the error was gone.

Comments

0

DUPLICATE or AMBIGUOUS header or implementation files

This can occur when there are multiple potential paths to the files you are importing. For example if you import MyClass.h, but there are two instances of MyClass.h in your project

Comments

0

my solution:

I have been imported some .h and .m files in my project. but have not used it any of my classes. so I removed those from finder. this caused the above error.

so I had to go the project setting/ build phases => then also remove those file references. they were in red color. because they have been deleted from finder not from Xcode

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.