undefined reference to _main

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
ubergeek
Posts: 23
Joined: Thu Aug 09, 2007 5:26 am

undefined reference to _main

Post 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.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

Do you have a function called _main?
ubergeek
Posts: 23
Joined: Thu Aug 09, 2007 5:26 am

Post by ubergeek »

Yes, there is an

Code: Select all

int main()
function in the C++ file. (which compiles succesfully)
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

Look at the symbols inside your C++ object with the nm command.. :wink:
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
Tyler
Member
Member
Posts: 514
Joined: Tue Nov 07, 2006 7:37 am
Location: York, England

Post by Tyler »

Person 1: Do you have a fish?

Person 2: Yes, there's a rabbit over there.
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

Post 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
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post 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:
Author of COBOS
Tyler
Member
Member
Posts: 514
Joined: Tue Nov 07, 2006 7:37 am
Location: York, England

Post 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!
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Post 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
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
blound
Member
Member
Posts: 70
Joined: Sat Dec 01, 2007 1:36 pm

Post by blound »

gcc do not append the _ so im assuming g++ doesnt either
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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.
Every good solution is obvious once you've found it.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post 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
User avatar
bluecode
Member
Member
Posts: 202
Joined: Wed Nov 17, 2004 12:00 am
Location: Germany
Contact:

Post 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.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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?
Post Reply