views:

305

answers:

3

Hi, I'm trying to build a project I have and it has several exported functions. The functions follow the stdcall convention and they get mangled if compiled with GCC as

Func@X

Other compilers mangle the name like this:

_Func@X

Is any way I can force GCC to mangle the names of the exported functions to the later example?

A: 

See the GCC manual regarding -fleading-underscore. Do read the warnings about the consequences of this action however; it may not be the solution you think it is.

Clifford
Thanks for the answer though it doesn't seem to work, so the standard in C is not to use the leading underscore? I was trying to make it kind of portable among compilers, the functions are exports of a DLL which need to be loaded.
JP
Are you compiling the code as C or C++? For compiler interoperability, the DLL must only present C interfaces. You can use C++, but the interfaces should be declared extern "C" (and of course only use C compatable types, and no function overloading).
Clifford
+1  A: 

The best bet when dealing with function name mangling on Windows is to always use a .def file. This will work the same regardless of the compiler. Typically you only need the EXPORTS section:

EXPORTS
  Func1
  Func2
  ...
Pavel Minaev
A: 

See this answer.

int Func() __asm__("_Func@X");

This will force GCC to name the symbol _Func@X regardless of what it would have done normally.


Oh right, @ is special: it's used for symbol versioning. I thought that __asm__("...@...") used to work, but I guess it doesn't anymore.

int Func() __asm__("_Func");
__asm__(".symver _Func, _Func@X");

This must be accompanied by a mapfile, something like

1 {
  global:
    _Func;
};

given to gcc -Wl,-Map=foo.map when linking.

ephemient
Thanks, but either I am doing something wrong or it doesn't work, it won't let me link it. http://pastebin.com/f14a9a00f
JP