7

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?

JP.
  • 71
  • 1
  • 2

3 Answers3

4

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 version script file, like:

1 {
  global:
    _Func;
};

given to gcc -Wl,--version-script=foo.version when linking.

Community
  • 1
  • 1
ephemient
  • 198,619
  • 38
  • 280
  • 391
  • 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. Nov 10 '09 at 07:40
0

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
  • 88,407
  • 13
  • 85
  • 165
  • 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. Nov 09 '09 at 21:11
  • 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 Nov 10 '09 at 11:30
0

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
  • 99,783
  • 25
  • 219
  • 289