1

I'm looking for a way to define an unmangled alias for an exported function in a DLL compiled with GCC.

According to this StackOverflow answer, Visual Studio supports

#pragma comment(linker, "/EXPORT:SomeFunction=_SomeFunction@@@23mangledstuff#@@@@")

to specify that an exported function shall have a given alias. I'm looking for something similar in GCC. The GCC function attribute docs suggest __attribute__ ((alias ("target"))) will do the trick, but this just generates a mangled function name:

#undef DLL_EXPORT
#ifdef CCDLL_EXPORTS
#define DLL_EXPORT __stdcall __declspec(dllexport)
#else
#define DLL_EXPORT __stdcall __declspec(dllimport)
#endif

DLL_EXPORT CCDLL_v1* GetCCDLL_premangled();

DLL_EXPORT CCDLL_v1* GetCCDLL () __attribute__ ((alias ("_Z19GetCCDLL_premangledv")));

and here's the .def file from the resulting DLL:

EXPORTS
    _Z19GetCCDLL_premangledv @1
    _Z8GetCCDLLv @2

The original function is exported and mangled as expected, but so is the function alias. If I drop the DLL_EXPORT from the function alias, I expect it would remain unmangled, but it's also not exported from the DLL. I thought the stdcall calling convention might be mangling the function alias, but when I removed it from the DLL_EXPORT definition, the .def file remained exactly the same.

This SO answer suggests I can define an alias using --defsym=alias_name=target, but ld doesn't seem to recognize this and I can't find anything in the official GCC linker docs at all.

Is there a way to do what I'm looking for, or does GCC just not support unmangled aliases for exported functions?

Community
  • 1
  • 1
cf stands with Monica
  • 7,919
  • 8
  • 29
  • 56

1 Answers1

2

Did you try in your .def file:

normalName=mangled_name @ 1

Another way may be to define an extern "C" function that calls the internal C++ one, and export that.

DNT
  • 2,196
  • 12
  • 15
  • Be careful, anything that (directly or indirectly) uses C++ features **will not work** when called from plain C. – vonbrand Mar 22 '14 at 23:31
  • The .def file worked out perfectly. I'd been setting it up wrong; thanks for showing the proper way to do it. – cf stands with Monica Mar 23 '14 at 03:47
  • @vonbrand Can you develop on that? I have been using for some years `extern "C"` wrappers around C++ functions to use opencv which is C++ from my C code. The only thing I needed was to use `void *` in the interface to pass pointers to C++ `class`es around C functions. Everything works fine even with `-O3 -flto`, so from my experience, **it will work**. – alx Apr 14 '20 at 20:11