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!