简体   繁体   English

在 Visual Studio 2010 C++ 项目中使用 MinGW 构建的 dll

[英]Using MinGW built dll in a Visual Studio 2010 C++ project

I have build a dll and now I want to use this dll in a Microsoft Visual Studio project.我已经构建了一个 dll,现在我想在 Microsoft Visual Studio 项目中使用这个 dll。

g++ -O0 -Wall -c -fmessage-length=0 -osrc\MyLib.o ..\src\MyLib.cpp
g++ -shared -Wl,--out-implib=MyLib.lib -Wl,--output-def=MyLib.def -oMyLib.dll src\MyLib.o -lwsock32

The dll works fine when I use it in a "gcc project".当我在“gcc 项目”中使用 dll 时,它可以正常工作。

I have tried different methodes to create the ".lib" and ".def" files and tried to import these libs in VS by following different tutorials.我尝试了不同的方法来创建“.lib”和“.def”文件,并尝试按照不同的教程在 VS 中导入这些库。 But VS does not find the methodes declared in the dll...但是VS没有找到dll中声明的方法...

I'm thankfull for any help.我很感激任何帮助。

Have you heard about name mangling?你听说过名字修饰吗? Unless the functions exported from DLL are marked as extern "C" their names are going to be mangled in a compiler-specific way.除非从 DLL 导出的函数被标记为 extern "C",否则它们的名称将以编译器特定的方式被破坏。 Thus the problem.因此问题。

For the benefit of others.为了他人的利益。 Have a look at Binary Compatible C++ Interfaces .看看二进制兼容的 C++ 接口 Its clear from the above that there is no way direct to interface C++ DLL built with MinGW with MS Visual Studio.从上面可以清楚地看出,没有办法直接将使用 MinGW 构建的 C++ DLL 与 MS Visual Studio 接口。 This interfacing "works" cleanly only via C based interface.这种接口只能通过基于 C 的接口干净地“工作”。

If you want your dll to be used with another compiler you have the following options:如果您希望您的 dll 与另一个编译器一起使用,您有以下选项:

  1. Expose only "C" interface (with extern "C") - no classes or anyting C++ specific.仅公开“C”接口(带有外部“C”)- 没有类或任何特定于 C++ 的。
  2. Make a COM dll.制作一个 COM dll。
  3. Make your on COM-like model that follows the same constraints.制作遵循相同约束的类似 COM 的模型。

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

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