Question regarding linking assembly and C

Programming, for all ages and all languages.
Post Reply
User avatar
babernat
Member
Member
Posts: 42
Joined: Tue Jul 03, 2007 6:53 am
Location: Colorado USA

Question regarding linking assembly and C

Post by babernat »

All,

I'm trying to link two binary files one was created via nasm and the other via gcc. In the assembly file I reference the function in the C code with an underscore:

extern _main
call _main

My function in my c file is defined as

void main...

When I attempt to link the files using ld, I get the following error:

undefined reference to `_main'

The only way I have gotten this to link is by giving gcc the fno-leading-underscore and removing the underscore from my assembly.

Which I guess is fine, but I was under the impression that gcc will prepend functions with an underscore and that is why I put the underscore in there. In fact, if I compile under djgpp in windows, then I have no problems at all using the underscore. Am I doing something wrong or is this the way it was meant to be and I'm just misunderstanding the whole gcc underscore thing?

The software versions are:
GNU ld version 2.17.50.0.5 20060927 (SUSE Linux)
gcc (GCC) 4.1.2 20061115 (prerelease) (SUSE Linux)
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

You are correct. GCC should not put a underscore in front of function names by default. It has something to do with the GCC ABI for Linux. It is just standard as far as I know, unless someone else can explain it better.

Just for extra help do objdump -x <your C object file before linking> to determine how the function symbols need to be referenced. You should notice that there are no underscores like you stated above.
User avatar
babernat
Member
Member
Posts: 42
Joined: Tue Jul 03, 2007 6:53 am
Location: Colorado USA

Post by babernat »

That makes sense to me. I wanted to make sure I wasn't going crazy. Thanks for the tip on objdump -x that pretty much says it all.
Post Reply