Xcode duplicate symbol _main
Asked Answered
C

11

10

I'm getting the following error in Xcode 3.2.1 on Snow Leopard 10.6.2 whenever I try to compile any iPhone application generated by Appcelerator Titanium. However, the build error only appears when I select iPhone simulator on the architecture menu and if I select the iPhone device, I am able to run the app on my device .

Further more, the iPhone simulator launches successfully and executes the program directly from the Titanium environment which uses Xcode to build .

Why is this happening ?

ld: duplicate symbol _main in Resources/libTitanium.a(main.o) and /Users/prithviraj/Documents/project/Final/build/iphone/build/Final.build/Debug-iphonesimulator/Final.build/Objects-normal/i386/main.o collect2: ld returned 1 exit status Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1

Circumscissile answered 29/3, 2010 at 0:58 Comment(0)
U
19

I can't tell you why this is happening, but I can suggest a workaround. When I commented out the entire contents of the main.m file that was generated for my Titanium project, I was able to compile successfully and run on the Simulator. Let me know if that works for you.

Unideaed answered 29/3, 2010 at 1:9 Comment(4)
As a clarification, the reason why you see the error is that libTitanium, the primary Titanium library file, contains a main entry point and this is therefore redundant to the main function in main.m. I'm not sure why this problem doesn't crop up elsewhere, but it must be something with the way the compiler is invoked by Titanium, to hook into their entry point instead of the one in main.m.Unideaed
In my case, a library I'm using had it's own main.m. Commented out and it workedBalalaika
While that works, I think this is the correct way to handle this: https://mcmap.net/q/102798/-xcode-duplicate-symbol-_mainRocray
No disagreement from me on that. I suppose I had less experience with Build Phases last year, which explains how I overlooked the obvious solution.Unideaed
A
11

Check to see if you have multiple declarations of main function in your project.

Ant answered 30/1, 2011 at 6:54 Comment(0)
B
8

I just spent a couple hours battling this one. It was because I was using the -all_load linker flag. If you are using that flag to get around the category bug, there are some other solutions -- see here.

Bhakti answered 2/2, 2011 at 17:5 Comment(2)
#933356, the link suggest to use -ObjC.Hyperkinesia
This was the cause in a our project. It was an older project where -all_load was being used and we just linked a new library in. The result was over 200 duplications. Removing -all_load fixed it.Takeoff
P
6

I had a similar problem. A unit test class was accidentally included in my build. If you search your project for "main(" you'll probably find the duplicate functions.

Peyote answered 7/4, 2011 at 15:48 Comment(0)
C
1

This happened to me for 2 reasons:

1: Class A calls class B and both had imported the same class. Fix it by importing the class in .m file.

2: Two classes have a constant with the same name (even if the constant is defined in the .m file). Fix it by changing the name of the constants.

Colin answered 14/8, 2012 at 21:30 Comment(0)
R
1

I had this problem because I define a file as such:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
... 
} 

And also had a file main.m:

int main(int argc, char* argv[])
{
    @autoreleasepool {
        int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
        return retVal;
    }
}
Rivalee answered 8/8, 2015 at 21:23 Comment(0)
Z
0

I found this happened when i had an implementation file with a main function in it (say abc.m) and also had another main.m. Once I commented out the main function in abc.m, the project compiled successfully.

Zipnick answered 19/2, 2011 at 0:32 Comment(0)
B
0

Seems as though there are multiple ways to get into this state. Mine was different. I read a hint where you could drag from a .xib event selector into the .h implementation of your view controller and it would auto-gen your methods. It did - which was cool. I immediately started getting duplicate symbol errors - which was not cool.

I did not have time to dig deep into the linker to see what happened. I created a new view controller, copied the context of my old .xib into the new guy. Deleted the old .h, .m and .xib and built and it worked again. Very odd, very annoying time waste.

There is obviously some bug with this xcode "convenience".

Billibilliard answered 15/9, 2012 at 14:46 Comment(0)
L
0

Based off what I can tell from these other answers, I'm going to need to be remove a bunch of main methods.

But to do that easily I first need to remove all the gd comments from my files because they are assiduously documented with comments at nearly every other line

This regular expression matches all C multi-line comments including their delimeters & may help you on your journey

/\*((?!\*/).)*\*/
Lade answered 24/2, 2014 at 13:42 Comment(0)
I
0

I spent more than an hour in searching for a correct answer but nothing worked for me. Finally the xcode it self telling something is duplicated, so go to that particular folder(in this case: /Users/prithviraj/Documents/project/Final/build/iphone/build/Final.build/Debug-iphonesimulator/Final.build/Objects-normal/i386/main.o) and delete all the files and also check same in your project whether that particular class/interface is declared twice if yes delete it.

After deletion clean and run the project.

It worked for me hope this helps(-_-).

Irish answered 18/5, 2017 at 9:58 Comment(0)
M
-1

Delete /Users/{username}/Library/Developer/Xcode/DerivedData folder and build again.

Maynord answered 23/10, 2017 at 21:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.