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?

1 Answer 1

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.

4
  • Be careful, anything that (directly or indirectly) uses C++ features will not work when called from plain C.
    – vonbrand
    Mar 22, 2014 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-
    Mar 23, 2014 at 3: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++ classes around C functions. Everything works fine even with -O3 -flto, so from my experience, it will work. Apr 14, 2020 at 20:11
  • @vonbrand on multiple platforms including MS Windows libc functions are themselves implemented in C++ so I'd cast a doubt on this Mar 6 at 2:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.