I declared some C++ functions prototyped as follows:
extern "C" void __stdcall function();
I also have some third-party dll with exported function() - no name decorations at all.
I'm unable to build my exe or dll due to undefined reference to function@..., because of MinGW's stdcall @-suffix. How can I obtain object file without @... just plain function names?
It sounds like you're trying to use MinGW to compile a program that uses external C functions from a third-party dll. There's a way to export those external functions into a proper import library that MinGW's
gnu ldlinker can use but it involves creating a.defdefinition file. The advantage with this is once you create a proper import library you won't have to fiddle around with switches like--add-stdcall-aliasor--kill-atbecause the import library will contain the symbols expected by the compiler and linker.Here's a rough outline of the procedure for doing this:
dlltool.exewhich should be included in the sameMinGW/bindirectory as the compiler.Here's how the definition file looks like:
Couple of important things to note. The
EXPORTSsection has to list the exported symbols/functions in the same name-decorated format as expected by the toolchain. In this case, MinGW compiler and ld linker expects__stdcallC functions to be appended with an '@' follow by the number of bytes in the arguments. The second important thing to note is thatdlltool -kwill remove the '@', which does the same thing as the--kill-atoption you've already seen. The end result of this is you have an import library with the correct internal name-decoration, so things resolve properly, and that internal name will map to the exported visible name found in your 3rd party dll.One final thing that needs mentioning. Throughout the entire example, we were assuming the undecorated names in the dll used the
__stdcallwhich isn't necessarily true. The following chart(taken from here) shows how different compilers decorate__cdeclvs__stdcalldifferently:It's up to you to make sure that the call conventions match-up properly or risk stack corruption and mysterious program crashes.