简体   繁体   English

VisualStudio和gcc之间* .dll * .a * .lib * .def的兼容性

[英]Compatibility of *.dll *.a *.lib *.def between VisualStudio and gcc

this is very confusing. 这非常令人困惑。 I spent a lot of time reading posts on this on stack, etc. Still confused. 我花了很多时间在堆栈上阅读帖子,等等。仍然困惑。

I am using Qt and C++ for coding. 我正在使用Qt和C ++进行编码。 In Qt, I am using the gcc option for a compiler. 在Qt中,我使用gcc选项编译器。
The problem is that many 3rd party libraries that I've tried do not seem to work. 问题是我尝试过的许多第三方库似乎都不起作用。

I am new to .dll, .a, .lib, .def files and library schemes. 我是.dll,.a,.lib,.def文件和库方案的新手。

Question 1: 问题1:

In my limited experience (I've tried 7 or 9 libraries so far), suppliers of libraries seldom tell you whether the .dll was made with VisualStudio or gcc. 在我有限的经验中(到目前为止我已经尝试了7或9个库),图书馆的供应商很少告诉你.dll是用VisualStudio还是用gcc制作的。 This adds a lot of confusion. 这增加了很多困惑。 They almost never make it clear what compiler the library is compatible with. 他们几乎从不清楚库兼容的编译器。 So I would appreciate some real life tips of how to deal with this nightmare. 所以我很欣赏一些如何应对这场噩梦的真实生活技巧。 Almost all the libraries I tried are OpenSource projects. 我尝试过的几乎所有库都是OpenSource项目。 I won't name names here, but these are well known projects. 我不会在这里命名,但这些都是众所周知的项目。 I'm sure the problem is my lack of knowledge... 我确定问题是我缺乏知识......

MinGW and gcc World MinGW和gcc World

Question2: 问题2:
As far as I can tell, dynamic C++ libraries for MinGW gcc universe require these, right? 据我所知,MinGW gcc Universe的动态C ++库需要这些,对吧?
*.h *。H
*.dll * .DLL
*.a *。一种

Question 3: 问题3:
Unfortunately, the .a file is often missing and the library does not work. 不幸的是,.a文件经常丢失,库不起作用。 This is very confusing. 这非常令人困惑。 If the .a file is missing am I out of luck? 如果.a文件丢失,我运气不好?

Question 4: 问题4:
Can I generate the .a file for MinGW/gcc if the *.dll was made with gcc? 如果* .dll是用gcc制作的,我可以为MinGW / gcc生成.a文件吗?

Question 5: Can I generate the .a file for MinGW/gcc if the *.dll was made with VisualStudio? 问题5:如果* .dll是用VisualStudio制作的,我可以为MinGW / gcc生成.a文件吗?

Question 6: 问题6:
Is it possible that a *.dll (made with MinGW/gcc) is too old and no longer compatible with newer MinGW/gcc? 是否有可能* .dll(由MinGW / gcc制作)太旧而且不再与较新的MinGW / gcc兼容?

Question 7: 问题7:
Qt projects using MinGW/gcc never need *.lib files, right? 使用MinGW / gcc的Qt项目从不需要* .lib文件,对吧? That is a VisualStudio only thing, right? 那只是VisualStudio的一件事,对吧?

Question 8: 问题8:
I don't need a *.def file to use a *.dll in a Qt projects using MinGW/gcc, right? 我不需要* .def文件在使用MinGW / gcc的Qt项目中使用* .dll,对吗?

VisualStudio World VisualStudio世界

Question 9: 问题9:
As far as I can tell, dynamic C++ libraries for VisualStudio require these: 据我所知,VisualStudio的动态C ++库需要这些:
*.h *。H
*.dll * .DLL
*.lib * .LIB

Right? 对? Again, the problem is that the *.lib file is almost always missing. 同样,问题是* .lib文件几乎总是丢失。 Plus, no clear instructions about what compiler the library is compatible with. 另外,没有关于库兼容的编译器的明确说明。 So how can I know that it is for VisualStudio only or not? 那么我怎么知道它只适用于VisualStudio呢?

Question 10: 问题10:
If the .lib file is missing am I out of luck? 如果缺少.lib文件,我运气不好?

Question 11: 问题11:
Can I generate the .lib file for VisualStudio if the *.dll was made with VisualStudio? 如果* .dll是用VisualStudio制作的,我可以为VisualStudio生成.lib文件吗? How? 怎么样?

Question 12: 问题12:
Can I generate the .lib file for VisualStudio if the *.dll was made with MinGW/gcc? 如果* .dll是用MinGW / gcc制作的,我可以为VisualStudio生成.lib文件吗? How? 怎么样?

Question 13: 问题13:
Is it possible that a *.dll (made with VisualStudio) is too old and no longer compatible with newer VisualStudio? 是否有可能* .dll(使用VisualStudio制作)太旧而且不再与较新的VisualStudio兼容?

Question 14: 问题14:
If in QtCreator I select the VisualStudio compiler, is that 100% compatible with dynamic libraries compiled with REAL VisualStudio by someone else? 如果在QtCreator中我选择了VisualStudio编译器,那是否与其他人使用REAL VisualStudio编译的动态库100%兼容? I believe the VisualStudio compiler option in Qt Creator is a fake VisualStudio compiler. 我相信Qt Creator中的VisualStudio编译器选项是伪造的VisualStudio编译器。

Question 15: 问题15:
If in QtCreator I select the MinGW/gcc compiler, can I use with Qt dynamic libraries compiled with REAL VisualStudio by someone else? 如果在QtCreator中我选择MinGW / gcc编译器,我可以使用其他人使用REAL VisualStudio编译的Qt动态库吗?

Question 16: 问题16:
I don't need a *.def file to use a *.dll in a Qt projects using MinGW/gcc, right? 我不需要* .def文件在使用MinGW / gcc的Qt项目中使用* .dll,对吗?

Question 17: Can I convert a *lib (that works with a *.dll and *.h) file made with REAL VisualStudio to a *.a file so I can use the *.a file with the unmodified *.dll, and *.h files in a Qt gcc project? 问题17:我可以将使用REAL VisualStudio制作的* lib(使用* .dll和* .h)文件转换为* .a文件,这样我就可以将* .a文件与未修改的* .dll一起使用,并且Qt gcc项目中的* .h文件?

Maybe it is worth starting at the beginning and not jump ahead of ourselves and describe the core issue. 也许它值得从一开始就开始,而不是超越自我并描述核心问题。 From this answers to several of the questions can be derived. 从这个答案中可以得出几个问题。

The start is the ABI (application binary interface). 起点是ABI(应用程序二进制接口)。 This defines things like 这定义了类似的东西

  • how a function is called, eg which parameters go into which registers or what location on the stack they put 如何调用函数,例如哪些参数进入哪些寄存器或堆栈中的哪个位置
  • how exceptions are thrown 如何抛出异常
  • how objects are layed out, eg where the "vtable pointer" goes, what padding is used 如何布置对象,例如“vtable指针”所在的位置,使用什么填充
  • how big the build-in data types are 内置数据类型有多大
  • how the function names are "mangled" into symbols 函数名称如何“损坏”成符号
  • how type information is layed out 如何打印类型信息
  • the layout of standard library classes 标准库类的布局
  • etc. 等等

Most platforms define a C ABI but don't define a C++ ABI. 大多数平台定义了C ABI,但没有定义C ++ ABI。 As a result compiler define their own ABI (for everything except the C stuff which is typically there). 结果编译器定义了他们自己的ABI(除了通常存在的C之外的所有东西)。 This yields object files which are incompatible between different compilers (sometimes even between versions of the same compiler). 这会产生在不同编译器之间不兼容的目标文件(有时甚至在同一编译器的版本之间)。

Typically, this manifests itself in strange-looking names somehow being undefined: different ABIs deliberately use different name mangling to prevent accidentally linking an executable which won't work anyway. 通常情况下,这表现为看起来很奇怪的名称,某种程度上是未定义的:不同的ABI故意使用不同的名称修改来防止意外链接无法正常工作的可执行文件。 To work around these your best bet is to build all components using the same compiler. 要解决这些问题,最好的办法是使用相同的编译器构建所有组件。

If you want to determine which compiler a library is build with, you can have a look at its contents using appropriate tools. 如果要确定构建库的编译器,可以使用适当的工具查看其内容。 I realize that you asked for Windows but I only know the UNIX tools (they may be available with MingW): 我意识到你要求Windows,但我只知道UNIX工具(它们可能与MingW一起提供):

  • nm to look at the symbol names (typically together with less or grep) nm来查看符号名称(通常与less或grep一起)
  • ar to build or inspect libraries 建立或检查图书馆
  • ident to find special strings embedded in the object ident用于查找嵌入在对象中的特殊字符串
  • strings to fond all strings 字符串喜欢所有字符串
  • c++filt to demangle symbols into their C++ declaration c ++ filt将符号解码为C ++声明

Looking at the symbols typically yields identifications of what compiler produced them. 查看符号通常会产生编译器生成它们的标识。 If you have seen them suffiently often, you can even tell the ABI from the symbols themselves. 如果你经常看到它们,你甚至可以从符号本身告诉ABI。

There is lots more in this area but I've run out of stamina... :-) In any case, I think this answers several of the questions above. 在这个领域还有很多,但我已经筋疲力尽...... :-)无论如何,我认为这回答了上面的几个问题。

I stumbled upon this question when searching for the tool to use to create the .a file using Code::Blocks c++ compiler for windows. 在使用Code :: Blocks c ++ compiler for windows搜索用于创建.a文件的工具时,我偶然发现了这个问题。 Code:Blocks uses MinGW gcc compiler. 代码:Blocks使用MinGW gcc编译器。 It was high enough on google to validate my necromancy I think. 在谷歌这是足够高,以验证我的神秘感。

Dynamic link libraries (dll's) are a mixed bunch. 动态链接库(dll)是混合的。 Some can be compiled in a way that makes them very hard to use outside the programming language and compiler they were created with. 有些可以编译,使得它们很难在编程语言和创建它们的编译器之外使用。

Often however the dll is created with a clean C interface. 但是,通常使用干净的C接口创建dll。 When that is the case the answers to your questions that I think I can answer is: 在这种情况下,我认为我能回答的问题的答案是:

1: that's not a question. 1:那不是问题。

2, 9: yes 2,9:是的

3, 10: no 3,10:不

4, 11: yes. 4,11:是的。 MinGW includes a tool (dlltool.exe) that takes a .dll and a .def file and creates a .a file MS VisualStudio also includes a tool (that I think is called lib.exe) to do the same thing. MinGW包含一个工具(dlltool.exe),它接受.dll和.def文件并创建.a文件MS VisualStudio还包括一个工具(我认为称为lib.exe)来做同样的事情。 And if you start using another compiler you will probably find they have a tool too. 如果你开始使用另一个编译器,你可能会发现他们也有一个工具。 Borlands compilers had the implib.exe tool. Borlands编译器有implib.exe工具。

5, 12: yes (same as 4) 5,12:是(与4相同)

6, 13: pew... I don't think there is an expiration date on dll's but they must be compiled for the right operating system. 6,13:pew ...我不认为dll上有过期日期,但必须为正确的操作系统编译它们。

8, 16: you need the .def to make the .a or .lib, if you don't have it, it is actually posible to create that from the .dll 8,16:你需要.def来创建.a或.lib,如果你没有它,它实际上是可以从.dll创建的。

A DLL is essentially a compiled application - just in the form of a function library rather than an EXE file. DLL本质上是一个已编译的应用程序 - 只是以函数库的形式而不是EXE文件的形式。 Any other application can use the functions within that DLL by just declaring the function, the dll containing the function, and the parameters and return values and such. 任何其他应用程序都可以通过声明函数,包含函数的dll以及参数和返回值等来使用该DLL中的函数。

DLLs must already exist on a system if an application is compiled using "dynamically linked libraries", so you must either include the necessary DLLs in your installer, or hope that they already exist on the target computer. 如果使用“动态链接库”编译应用程序,则DLL必须已存在于系统上,因此您必须在安装程序中包含必需的DLL,或者希望它们已存在于目标计算机上。 Using DLLs makes your app's size smaller overall. 使用DLL可以使您的应用程序整体更小。

Creating DLLs is just like creating any other application - you just target your build as a DLL rather than an EXE or whatever. 创建DLL就像创建任何其他应用程序一样 - 您只需将构建定位为DLL而不是EXE或其他任何东西。

To create any application - DLL, EXE or otherwise - you need the necessary source code and headers. 要创建任何应用程序 - DLL,EXE或其他 - 您需要必要的源代码和标头。 .h files contain declarations for functions and data types and classes and whatnot - they rarely contain code. .h文件包含函数和数据类型和类的声明以及诸如此类的东西 - 它们很少包含代码。 A .def is a lot like a .h, but usually a set of instructions for a linker. .def很像.h,但通常是一组链接器的指令。

When you compile, a .h or .c or whatever turns into a .obj - an object file. 编译时,.h或.c或其他任何变成.obj - 目标文件。 Multiple object files are linked together to create your DLL or EXE. 多个目标文件链接在一起以创建DLL或EXE。

A .lib file is a static library - essentially a bunch of .obj files (or one .obj) that have been combined for the linking stage. .lib文件是一个静态库 - 本质上是一组.obj文件(或一个.obj),它们已经被组合用于链接阶段。

The format of .obj and .lib files can be particular to a compiler, and they are rarely compatible between compilers. .obj和.lib文件的格式对于编译器来说可能是特定的,并且它们在编译器之间很少兼容。 You must have the original source code, or an .obj or .lib made specifically for your compiler. 您必须具有原始源代码,或者专门为您的编译器生成的.obj或.lib。

When you choose to make an EXE with "dynamically linked libraries", it will be expecting DLLs that it can use. 当您选择使用“动态链接库”创建EXE时,它将期望它可以使用的DLL。 When you choose "statically linked libraries", the linker will locate the .lib files it needs before producing the EXE, and you won't need those DLLs. 当您选择“静态链接库”时,链接器将在生成EXE之前找到所需的.lib文件,并且您不需要这些DLL。

question 1: you should import .h file and link .a file by linker command and copy .dll near your .exe output. 问题1:您应该通过链接器命令导入.h文件并链接.a文件,并在.exe输出附近复制.dll

question 2: you can make .a file by .def file 问题2:您可以通过.def文件制作.a文件

set PATH=C:\Program Files\CodeBlocks\MinGW\bin;%PATH%

dlltool.exe -d libfftw3-3.def -l libfftw3-3.a

question 3: no. 问题3:没有。 you can make .def file manually and after make .a file. 你可以手动制作.def文件,然后制作.a文件。

question 4,5: yes 问题4,5:是的

question 6: I think it is depend on your hardware and operation system not on your compiler. 问题6:我认为这取决于您的硬件和操作系统,而不是您的编译器。

question 7: I don't know. 问题7:我不知道。

question 8: you need only .h .a .dll not .def 问题8:你只需要.h .a .dll而不是.def

question 9: .lib files is for visual studio. 问题9: .lib文件适用于visual studio。

question 10: no you need .def and .dll to make .lib and you can make . def 问题10:不需要.def.dll来制作.lib ,你可以制作. def . def yourself if you don't have it. . def自己,如果你没有它。

set PATH=C:\Program Files\Microsoft Visual Studio 12.0\VC\bin;%PATH%

lib /machine:x86 /def:libfftw3-3.def

or 要么

lib /machine:x64 /def:libfftw3-3.def

Question 11: yes i told you above. 问题11:是的,我告诉过你。

question 12: yes 问题12:是的

question 13: no. 问题13:不。

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

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