Page 1 of 2

undefined reference to _main

Posted: Thu Dec 13, 2007 2:50 pm
by ubergeek
All right, so I'm trying to combine ASM and C++, and ld gives me an error:

Code: Select all

undefined reference to _main
I'm using g++ and NASM. I did put "extern _main" in the ASM file.

Posted: Thu Dec 13, 2007 3:00 pm
by Candy
Do you have a function called _main?

Posted: Thu Dec 13, 2007 3:26 pm
by ubergeek
Yes, there is an

Code: Select all

int main()
function in the C++ file. (which compiles succesfully)

Posted: Thu Dec 13, 2007 3:33 pm
by Brynet-Inc
Look at the symbols inside your C++ object with the nm command.. :wink:

Posted: Thu Dec 13, 2007 3:35 pm
by Tyler
Person 1: Do you have a fish?

Person 2: Yes, there's a rabbit over there.

Posted: Thu Dec 13, 2007 3:38 pm
by Craze Frog
ubergeek wrote:Yes, there is an

Code: Select all

int main()
function in the C++ file. (which compiles succesfully)
Read about C++ name mangling: http://en.wikipedia.org/wiki/Name_mangl ... in_C.2B.2B

Posted: Thu Dec 13, 2007 3:50 pm
by os64dev
Tyler wrote:Person 1: Do you have a fish?
Person 2: Yes, there's a rabbit over there.
Might i inquire the reasoning of this remark? Lets try to keep on track. At one point in time you also would have learned about extern "C" before int main to compile C++ executables.

Woo how subtle sniffed in an answer in a friendly nudge. :wink:

Posted: Thu Dec 13, 2007 3:56 pm
by Tyler
os64dev wrote:
Tyler wrote:Person 1: Do you have a fish?
Person 2: Yes, there's a rabbit over there.
Might i inquire the reasoning of this remark? Lets try to keep on track. At one point in time you also would have learned about extern "C" before int main to compile C++ executables.

Woo how subtle sniffed in an answer in a friendly nudge. :wink:
Well i was going to say objdump, but Brynet got in there and posted another method of actually checking the symbol name, so i just threw that in as a little joke post thinking that the topic would end there... apparenly not as funny as it sounded in my head...

Kudos on the answer in the form of a nostalgic statement!

Posted: Thu Dec 13, 2007 5:49 pm
by piranha
I had trouble with this too, and I fixed it by calling "main" not "_main" from the ASM file, and changing the extern statement to "main" from "_main"...maybe you could try that...

-JL

Posted: Thu Dec 13, 2007 7:34 pm
by blound
gcc do not append the _ so im assuming g++ doesnt either

Posted: Fri Dec 14, 2007 2:09 am
by Solar
blound wrote:gcc do not append the _ so im assuming g++ doesnt either
:roll:

GCC does whatever you told it in its configuration.

Posted: Fri Dec 14, 2007 2:33 am
by JamesM
piranha wrote:I had trouble with this too, and I fixed it by calling "main" not "_main" from the ASM file, and changing the extern statement to "main" from "_main"...maybe you could try that...

-JL
The question has been answered already... ;) C++ mangles identifiers. extern "C" will fix it.

Posted: Fri Dec 14, 2007 3:04 am
by AJ
I don't think g++ mangles the name of the main function (as long as it is a traditional int main()) by default - I've followed the cross-compiler tutorials and that certainly leaves you with a compiler that produces the symbol 'main' whether or not you use extern "C".

Cheers,
Adam

Posted: Fri Dec 14, 2007 8:48 am
by bluecode
The leading underscore has nothing to do with C++ name-mangling. It is possible to use the -fleading-underscore / -fno-leading-underscore option with gcc and g++ (iirc there is also a configure switch for that, look for yourself), look into the gcc manuals.

Posted: Fri Dec 14, 2007 9:56 am
by JamesM
bluecode wrote:The leading underscore has nothing to do with C++ name-mangling. It is possible to use the -fleading-underscore / -fno-leading-underscore option with gcc and g++ (iirc there is also a configure switch for that, look for yourself), look into the gcc manuals.
I know this. What made you assume we didn't?