简体   繁体   English

为什么dll无法在C ++中使用?

[英]Why dll can't be used in c++?

It's pointed out by this answer: 该答案指出了这一点:

Failed to link mysql5.1.39\\bin\\libmySQL.dll 无法链接mysql5.1.39 \\ bin \\ libmySQL.dll

But I don't understand why, .dll is essentially the same as .lib except for there is only one copy of it used by different processes. 但是我不明白为什么, .dll.lib本质上是相同的,只是不同进程只使用一个副本。

Does it have anything to do with the IDE?I'm using visual c++ 2008 express 我在用Visual C ++ 2008 Express吗?

UPDATE UPDATE

Anyone know a free tool to convert .dll into .lib in windows? 有谁知道免费的工具,可以将.dll转换为Windows中的.lib

You are wrong on two counts. 你有两个错误。 Firstly, DLLs and LIBs (static libraries) are very different beasts. 首先,DLL和LIB(静态库)是完全不同的野兽。 The LIB you are talking about (I think) is the export library, which is simply a list of names in the DLL. 您正在谈论的LIB(我认为)是导出库,它只是DLL中名称的列表。 This library is typically produced when the DLL is compiled and is shipped with the DLL if the DLL is intended to be linked to by other developers. 该库通常在DLL编译时生成,并且如果DLL要与其他开发人员链接,则该库将与DLL一起提供。

To use a DLL with a modern IDE (I don't use VS) you typically include the matching .LIB (export library) in the project. 要将DLL与现代IDE(我不使用VS)一起使用,通常在项目中包含匹配的.LIB(导出库)。 At run-time you must make sure the DLL is available to your program - the simplest way to do this is to put the DLL in the same directory as the executable. 在运行时,您必须确保DLL可用于您的程序-最简单的方法是将DLL与可执行文件放在同一目录中。

And secondly, DLLs can be used with C++. 其次,DLL可与C ++一起使用。

DLL are specific windows executables which load on runtime. DLL是在运行时加载的特定Windows可执行文件。 Their equivalent in Linux is the *.so . 在Linux中,它们的等效值为* .so。
If you want to understand what's the difference between a DLL and a static lib see here . 如果您想了解DLL和静态库之间的区别, 请参见此处

Main reason has probably something to do with dll-file being an output of the linker (link.exe). 主要原因可能与dll文件是链接器(link.exe)的输出有关。 This command line utility doesnt know how to read dlls, only how to write them. 此命令行实用程序不知道如何读取dll,仅知道如何编写它们。

Actually sometimes .lib-files contain more than a list of functions. 实际上,有时.lib文件包含多个函数列表。 For example libpng.lib works as a standalone file, without any dll file. 例如,libpng.lib可以作为独立文件使用,而无需任何dll文件。

In order to use a DLL in C/C++ you need to link with what is called an import lib . 为了在C / C ++中使用DLL,您需要链接到所谓的import lib This is a small .lib that contains the names/ordinals the DLL exports in a format that the linker understands. 这是一个小.lib,其中包含DLL导出的名称/顺序(链接程序可以理解的格式)。 Once this has been linked in, the resulting binary will be able to use the DLL. 链接完成后,生成的二进制文件将可以使用DLL。 If you do not have the appropriate import lib, your C/C++ compiler should come with a tool to generate one from the DLL. 如果您没有适当的导入库,则您的C / C ++编译器应附带一个从DLL中生成一个的工具。

You can use the following analogy to understand the difference between a dll and a lib file. 您可以使用以下类比来理解dll和lib文件之间的区别。

the dll is like the source .cpp file while the lib is the header .h file. dll就像源.cpp文件,而lib是标头.h文件。

While this is not exactly true, the DLL holds the executable code while the LIB file tells the linker how to glue the code together. 尽管这不是完全正确,但DLL保留了可执行代码,而LIB文件告诉链接程序如何将代码粘合在一起。

It is also possible (in some cases) to generate the lib from the dll . 也有可能(在某些情况下)从dll生成lib Basically, all you need to know to call a function is the function entry point in the dll, the number of parameters and the size of each parameters. 基本上,您需要知道的所有调用函数的方法是dll中的函数入口点,参数数量和每个参数的大小。 Sending relevant information is then your own problem. 这样,发送相关信息就是您自己的问题。

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

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