开发者

Building dll with g++ for use with MSVC application

My end-goal here is to execute g++ from my MSVC application to build dlls at runtime. The dlls which g++ creates will then be loaded by the MSVC app for use.

Just messing around with some test code using the command line I managed to build a dll but it seems to have some problems:

C:\MinGW\bin>g++ -shared -o testdll.dll AIFuncs_RF.cpp AIFuncs_RF.def
Warning: resolving _CreateAIModule by linking to _CreateAIModule@4
Use --enable-stdcall-fixup to disable these warnings
Use --disable-stdcall-fixup to disable these fixups
Warning: resolving _DestroyAIModule by linking to _DestroyAIModule@0

Here is my def file:

LIBRARY "AIFuncs_RF"

EXPORTS

CreateAIModule=CreateAIModule @1
DestroyAIModule=DestroyAIModule @2

And the code for the dll main:

BOOL APIENTRY DllMain(HMODULE hModule, DWORD Reason, LPVOID pReserved)
{ switch ( Reason )
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}

return TRUE;
}

extern "C" void __stdcall
CreateA开发者_运维技巧IModule()
{
}

extern "C" void __stdcall
DestroyAIModule()
{
}

Any ideas why the functions aren't linked correctly?

Thanks a lot for any help.


The stdcall naming conventions of gcc and Microsoft's C compiler differ. The gcc adds the size of the arguments pushed onto the stack to the function name after an @. MS doesn't. Your def file contains the MS versions, so the gcc uses its auto-fixup to provide the MS style symbols.

This is per se not an error. gcc just warns you it has done such a thing, and you should make this explicit by providing the --enable-stdcall-fixup linker flag (so, -wl,--enable-stdcall-fixup to the compiler call).

The reason for the @ notation is BTW fairly sane: stdcall functions pop their arguments from the stack on return, and the caller pushes them, so they must agree perfectly on the size of these arguments or stack corruption will occur with disastrous results.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜