Xcode, Duplicate symbol _main
Asked Answered
L

10

58

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.

Loireatlantique answered 1/8, 2010 at 6:13 Comment(1)
This can happen if there are multiple occurrences of main() function.Dotti
L
92

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.

Loireatlantique answered 1/8, 2010 at 6:27 Comment(9)
I still dont have mine figured out... any other ideas anyone?Tanning
you really saved my lots of effortsAddam
I had a duplicate main from TBXML (which I just added)Alainealair
Thanks! If only I could give you more points... I'm starting to get enough of this XCode bullshit!Lobel
This can also happen if you accidentally #import "someFile.m" instead of #import "someFile.h"Barberabarberry
I have had a similar error when i recreated a subclass of NSManagedObject and gave the option to replace instead of just removing the file and recreating. Thanks for the tip.Cognomen
Wow! Lots of effort saved!Exurbia
it happened to me when i tried to import some other project code in my project. Xcode added main.m file from imported source code to compile sources for my parent project target. Thanks lot @huggie. you saved lot of time for me.Riviera
I needed to remove main.m because I was rewriting my AppDelegate in SwiftAcetal
P
23

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"
Patriarchy answered 10/10, 2012 at 13:19 Comment(3)
Thank you so much! After I tried all other solutions a simple search for .m" in my project showed the import of an .m file.Zetta
I had the same problem after changing around the inheritance hierarchy. Thanks.Ambassadress
In my case, I had multiple file "main.m" in my project. This answer helped me! --> https://mcmap.net/q/103399/-duplicate-symbols-for-architectures-in-xcodeWindbag
P
16

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

Pahoehoe answered 27/8, 2012 at 12:24 Comment(2)
i made a search by int main and i had two equal files, I removed one and project run. thank you ;)Wing
I ran grep -nr "int main" . and found a sample project within my project that came in as part of an SDK download that I added. After finding this out, I deleted those files via Xcode and voilà! The project now builds. Thanks.Jackscrew
A
8

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.

Arther answered 1/8, 2010 at 6:14 Comment(0)
S
3

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.

Supplemental answered 16/8, 2011 at 13:42 Comment(0)
V
3

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

Verger answered 1/12, 2011 at 13:33 Comment(0)
M
2

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 ;)

Meg answered 27/6, 2012 at 7:50 Comment(2)
Where should that be?Horrid
This is awesome, never seen this before. Just go to your project build phases, and you can find Validate Settings in Editor menu. Worked for me!Luscious
M
2

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.

Montcalm answered 4/8, 2014 at 23:14 Comment(0)
H
1

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.

Hersch answered 28/10, 2014 at 5:22 Comment(0)
E
0

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.

Endurant answered 9/10, 2014 at 0:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.