compiler/linker

Started by
5 comments, last by guywithknife 20 years, 1 month ago
If I have a function, say
int abc() {} 
what does gcc/g++ change the abc symbol to? From what I can gather (by looking at tutorials and the like) it should be _abc, but I cannot get that to work. I have the following assembly code:

[BITS 32]
[global start]
[extern _abc]

start:
  call _abc
 
But I get linker errors along the lines of: undefined reference to _abc, even though Im linking the object file generated after compiling my C++ code. By looking at the outputted assembly, I found it calls it something along the lines of __Z6abcv but when changing _abc to that in the assembly file, it gives me a simular error. Anything I have missed/should know or any tips to help me solve this problem would be greatly appreciated.
Advertisement
C++ uses (I am not kidding or exagerating) "name mangling" in the linker so it can use the same linker as C. Whether this was a good design decision is something of a long and flamey discussion.

If you want C style linking, precede your function name with __stdcall or extern "C". e.g.:

int __stdcall abc {} /* note two _ */

or

extern "C" int abc(){}
extern "C" is the proper way to do it.
Thanks for the replies.

However, I still can't get it working.

Using the __stdcall method I get an error when compiling (syntax error before '(') whilst with the extern "C" method seems to not make any difference (to the linking at least, though it does stop name mangling, at least the generated assembly does not have the abc mangled), yet it STILL does not compile, even though I have the same symbol as is generated...

So, Im guessing Im doing something wrong here... but I have no idea what.

Side note: If I use extern "C", will I still be able to use C++ code within that function?

[edited by - issch on March 18, 2004 9:43:16 PM]
are you making surwe to capitalise the c? extern "C"


Sharp Basic - Coming summer 2004!
typedef unsigned long long int me;
typedef signed short char you;
Yes, it is capitalised, that was a typo in my post..

By the way, in case it makes a difference, Im using Ld to link, DJGPP to compile the C/C++ code and Nasm to assemble the assembly.

[edited by - issch on March 18, 2004 9:47:10 PM]
Is there nothing else someone could tell me to get this working?
I have tried everything I could think of. I must have made a mistake someplace, but I cannot see where.

The names in my assembly source file now match the ones in the generated assembly from the c compiler (after adding extern "C"), yet the compiler still complains about undefined refferences. I dont get it at all, as I defined everything. It is as if the linker does not see the object file compiled from my C source file.

This topic is closed to new replies.

Advertisement