使用 g++ 构建 dll与 MSVC 应用程序一起使用
我的最终目标是从 MSVC 应用程序执行 g++ 以在运行时构建 dll。然后,MSVC 应用程序将加载 g++ 创建的 dll 以供使用。
只是使用命令行搞乱了一些测试代码,我设法构建了一个 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
这是我的 def 文件:
LIBRARY "AIFuncs_RF"
EXPORTS
CreateAIModule=CreateAIModule @1
DestroyAIModule=DestroyAIModule @2
以及 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()
{
}
有什么想法为什么这些函数没有正确链接吗?
非常感谢您的帮助。
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
CreateAIModule()
{
}
extern "C" void __stdcall
DestroyAIModule()
{
}
Any ideas why the functions aren't linked correctly?
Thanks a lot for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
gcc 和 Microsoft 的 C 编译器的 stdcall 命名约定不同。 gcc 将压入堆栈的参数大小添加到函数名 @ 后面。微软没有。您的 def 文件包含 MS 版本,因此 gcc 使用其自动修复来提供 MS 样式符号。
这本身并不是一个错误。 gcc 只是警告您它已经做了这样的事情,您应该通过提供
--enable-stdcall-fixup
链接器标志来明确这一点(因此,-wl,--enable-stdcall -fixup
到编译器调用)。@ 表示法的原因是相当理智的:stdcall 函数在返回时从堆栈中弹出它们的参数,调用者将它们压入,因此它们必须完全同意这些参数的大小,否则堆栈损坏将导致灾难性的结果。
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.