Find how to get this error

Jul 9, 2011 at 8:39pm
I have another challenge: write code that compiles perfectly but then gives this linker error:
Main.obj : fatal error LNK1179: invalid or corrupt file: duplicate COMDAT '_main'

Hint: Use main twice differently. Note that you will get a compiler error if you try to overload main. Removing the second or first use of main should end up calling main twice (meaning it compiles and links, and runs)
Last edited on Jul 9, 2011 at 8:40pm
Jul 9, 2011 at 11:51pm
Which "compiler" are you using?
Jul 9, 2011 at 11:54pm
The Visual C++ 2008 Express Edition compiler. I'm not sure whether the compiler matters, just a similar error message should suffice.
Jul 10, 2011 at 1:12am
In C::B if I call main inside main it compiles and runs, but crashes..
Jul 10, 2011 at 1:31am
closed account (zwA4jE8b)
Got me...
but thats not too hard to do.
I can call main in main and get a stack overflow but thats about it.
Jul 10, 2011 at 1:31am
Haha, infinite recursion is not considered a compile error unless you have infinite recursion templates. This challenge does not involve recursion ;)
Jul 10, 2011 at 1:42am
closed account (zwA4jE8b)
1>forum-help.obj : error LNK2005: _main already defined in f.obj
1>C:\Visual Studio 2010\C++\forum-help\Debug\forum-help.exe : fatal error LNK1169: one or more multiply defined symbols found

.. getting closer only 10 error messages away, lol

EDIT: Don't know enough about linking
Last edited on Jul 10, 2011 at 1:50am
Jul 10, 2011 at 1:52am
Neither do I, I just wrote some code to see what would happen and I got the linker error ;)
Hint: I only used one .cpp file and I only included <iostream> (it would still work without including iostream)
Last edited on Jul 10, 2011 at 1:53am
Jul 10, 2011 at 4:45pm
closed account (zb0S216C)
All I've managed to get was an access violation when returning from main (all I defined was a single integer (non-allocating)). LOL.

Wazzak
Jul 11, 2011 at 3:31am
I think I'll post the solution:
1
2
3
4
5
6
7
8
9
10
template<typename T>
int main()
{
	cout << "Hello, World!" << endl;

	cin.sync();
	cin.ignore();
}

int RunMainFunction = main<void>() + main<unsigned>();
If you comment out one of the two calls to main for the global variable, it will compile and link fine.
Jul 11, 2011 at 5:51am
error: cannot declare '::main' to be a template
error: ISO C++ forbids taking address of function '::main'
Jul 11, 2011 at 11:22am
Huh, what compiler are you using? It compiles perfectly in Visual C++ 2008 Express Edition.
Jul 11, 2011 at 12:57pm
That's not valid C++ since you cannot overload the main function.
Apparently VC++ fails to recognize this, so it leaves it to the linker to pick up the pieces.
Topic archived. No new replies allowed.