Another technical post for today.
This morning, one of the students in CS 526 encountered this error:
Build cocos2d libraries of project Sample with configuration Debug …
Ld build/Debug-iphonesimulator/Sample.app/Sample normal i386 …
ld: duplicate symbol .objc_class_name_HelloWorld in /Users/elliotle/Projects/Sample/build/Debug-iphonesimulator/libcocos2d libraries.a(HelloWorldScene.o) and /Users/username/Projects/Sample/build/Sample.build/Debug-iphonesimulator/Sample.build/Objects-normal/i386/HelloWorldScene.o
collect2: ld returned 1 exit status
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1
Well, it wasn’t exactly like that because I reproduced the error on my own computer so that I could post about it here
Here’s a screenshot of what the error looks like:
In the student’s case, he was trying to rename the HelloWorldScene class to MainMenu.
The problem comes from adding files to the project and having both the main target and the cocos2d library target checked. That results in the class being compiled twice, hence the duplicate symbol error.
Here’s how to fix it:
First, remove the offending files from the project:

Then, add them back, but make sure the cocos2d target is NOT checked:

That should do it.
Alternatively, you could just uncheck the cocos2d target for the offending class’s implementation file. But that’s not the path I happened to take when solving this problem, so I don’t have screenshots for it
Hope this helps!

Comments
So, it means that when you compile the sample, and cocos2d library will also be compiled even though you didn’t check that. What if you check it, you will compile that library twice and then got that kind of error?
is that right?
btw, am i the first person to post the comment?
Yes, you’re first! I’ll email you the code.
If you check the box, the class represented by the two files you’re adding will be compiled twice: once during the cocos2d library compilation step, and again when your own project target is compiled. Thus, there’s a duplicate symbol error.
This worked! Thanks so much
Thank you so much. This was a big help.
gcc-4.2 failed with exit code 1 i get this error could you tell me how to rectify it.
Hi,
unfortunately I can’t see the figures you attached becouse they aren’t avalaible now on your web site.
Please,could you send me them to my mail address I left you?
Thank you very much!
Caterina
Leave A Comment