简体   繁体   English

使用G ++构建DLL以与MSVC应用程序一起使用

[英]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. 我的最终目标是从我的MSVC应用程序执行g ++以在运行时构建dll。 The dlls which g++ creates will then be loaded by the MSVC app for use. 然后,由g ++创建的dll将由MSVC应用程序加载以供使用。

Just messing around with some test code using the command line I managed to build a dll but it seems to have some problems: 只是使用命令行弄乱了一些测试代码,我设法建立了一个dll,但似乎有一些问题:

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: 这是我的def文件:

LIBRARY "AIFuncs_RF"

EXPORTS

CreateAIModule=CreateAIModule @1
DestroyAIModule=DestroyAIModule @2

And the code for the dll main: 和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
CreateAIModule()
{
}

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. gcc的stdcall命名约定与Microsoft的C编译器不同。 The gcc adds the size of the arguments pushed onto the stack to the function name after an @. gcc将被压入堆栈的参数的大小添加到@之后的函数名称中。 MS doesn't. MS不会。 Your def file contains the MS versions, so the gcc uses its auto-fixup to provide the MS style symbols. 您的def文件包含MS版本,因此gcc使用其自动修复功能提供MS样式符号。

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). gcc只是警告您它已经做过这样的事情,您应该通过提供--enable-stdcall-fixup链接器标志(因此, -wl,--enable-stdcall-fixup到编译器调用)来明确表明这一点。

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. @表示法的原因是BTW相当理智:stdcall函数在返回时将其参数从堆栈中弹出,并且调用者将其推入,因此它们必须完全同意这些参数的大小,否则堆栈损坏将导致灾难性的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM