build failed with: ld: duplicate symbol _OBJC_CLASS_$_Algebra5FirstViewController

I am getting this error suddenly when running my app via iPhone simulator:

clang: error: linker command failed with exit code 1 (use -v to see invocation):

ld: duplicate symbol _OBJC_CLASS_$_Algebra5FirstViewController in .../Algebra5-anwcuftojtxtgkfootneeeqhwztj/Build/Intermediates/Algebra5.build/Debug-iphonesimulator/Algebra5.build/Objects-normal/i386/ExercisesViewController.o and .../Algebra5-anwcuftojtxtgkfootneeeqhwztj/Build/Intermediates/Algebra5.build/Debug-iphonesimulator/PSLE Algebra5.build/Objects-normal/i386/PSLE_Algebra5FirstViewController.o for architecture i386

What is it all about?


Solution 1:

It look like the class Algebra5FirstViewController is compile multiple time.

Can you make sure that the .m and .mm is only included once in your project sources in Xcode? You can also confirm this by checking in the compile log (last icon at the right, next to the breakpoints icon) and see that confirm that it is only compiled once.

Also, if this class is part of a library that you link against and you have a class with the same name, you could have the same error.

Finally, you can try a clean and rebuild, just in case the old object files are still present and there is some junk in the compiled files. Just in case...

EDIT

I also note that the second reference is made in the file for ExercisesViewController. Maybe there is something in this file. Either you #imported the Algebra5FirstViewController file instead of the .h, or the ExercisesViewController has @implementation (Algebra5FirstViewController) instead of @implementation (ExercisesViewController), or there was some junk with this file that will cleaned with a Clean an Rebuild.

Solution 2:

That can happen if the same .m file is referenced multiple times in your target's "Compile Sources" section in "Build Phases". Remove duplicate entries and you should be fine.

Solution 3:

also had this problem by declaring a const * NSString in the header file (incorrectly) instead of the implementation file (correctly)

Solution 4:

I got this issue because I accidentally imported the .m instead of the .h. Hope reading this saves someone with same problem some time.