Xcode, Duplicate symbol _main

Xcode

Xcode Problem Overview


I have duplicate symbol _main.

The problem is it says "Duplicate symbol _main in /Users/.../i386/main-B9843B6026D6EFA4.o and /Users/.../i386/main-B9843B6026D6EFA4.o", the XXX and XXX are actually the same .o file. I don't know why it thinks it's duplicate symbol when it's the same .o?!

Any help appreciated, thanks.

Xcode Solutions


Solution 1 - Xcode

Ah..I figure out it's that I have multiple entries under Targets/Compiled Sources ( in newer XCode it's under Build Phases/Compile Sources ). I removed them and the problem is solved. The multiple entry thing probably has to do with Git merge.

Solution 2 - Xcode

It appeared that in my case I was importing a .m file instead of its .h counterpart. Solved by editing

#import "Tools.m"

into

#import "Tools.h"

Solution 3 - Xcode

I also had this problem and it was caused by code I imported from another project. I did a grep for "int main" in my project directory:

grep -nr "int main" .

and found

./main.m:13:int main(int argc, char *argv[])
./IMPORTED_DIR/main.m:13:int main(int argc, char *argv[])

the IMPORTED_DIR contained the extra main.m that was causing the error for me

I removed that file from the Project -> Targets -> Build phases -> Compile sources list and then it compiled

Solution 4 - Xcode

I was facing the same issue with using two third party framework. (AppLovin and Flurry) And I came to know that by removing "all_load" from "Other Linker Flags" in build settings.

Solution 5 - Xcode

I had the same problem opening a project, that was created with Xcode 4.0.2, with Xcode 4.1. I simply solved by clicking on "Modernize Project" (Editor/Modernize Project). This procedure automatically removed all duplicates.

Solution 6 - Xcode

If still have a problem, try to search like this: "int main(", and remove those files except main.m

Solution 7 - Xcode

Just got this problem myself, but after reading huggie's solution which did lead me on the right track, I was left a bit confused. So current solution in Xcode: Choose project, target -> build phases and click validate settings

Then Xcode will auto fix its own mistake. It is always nice when the tools tries to stop your progress ;)

Solution 8 - Xcode

In my case, I declared an NSString in my constants file (imported by many classes) but forgot to define it as static!!

e.g. NSString* PARAMS = @"paramA"; should be: static NSString* PARAMS = @"paramA";

Reading the full error message allowed me to figure this out: "Duplicate symbol PARAMS". Don't be afraid and try to understand error messages! Sometimes they might even tell you exactly what you did wrong.

Solution 9 - Xcode

In my case, I had imported another project, in order to utilize a library contained within. It resulted my project having two main.m files.

This was even more confusing, since the error didn't show up until several builds later.

Solution 10 - Xcode

You can get this for method names too!

I got duplicate symbol _runOnMainQueueWithoutDeadlocking after adding DBCamera via CocoaPods and it was because both my category on NSObject (NSObject+Tools.h) and the GPUImage dependency file GPUImageOutput.m both had a method called 'runOnMainQueueWithoutDeadlocking'.

I was fortunate enough to be able to remove my method from code because I wasn't actually using it anymore or anywhere.

It's probably what I deserve for putting a category on NSObject.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhuggieView Question on Stackoverflow
Solution 1 - XcodehuggieView Answer on Stackoverflow
Solution 2 - XcodedvkchView Answer on Stackoverflow
Solution 3 - XcodeStefanView Answer on Stackoverflow
Solution 4 - XcodeArbaz ShaikhView Answer on Stackoverflow
Solution 5 - XcodeKas1aView Answer on Stackoverflow
Solution 6 - XcodeAltynbek UsenbekovView Answer on Stackoverflow
Solution 7 - XcodeInvulgoSoftView Answer on Stackoverflow
Solution 8 - XcodePierre-Francoys BrousseauView Answer on Stackoverflow
Solution 9 - XcodeFluffheadView Answer on Stackoverflow
Solution 10 - XcodekraftydevilView Answer on Stackoverflow