简体   繁体   中英

Why dll can't be used in c++?

It's pointed out by this answer:

Failed to link 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.

Does it have anything to do with the IDE?I'm using visual c++ 2008 express

UPDATE

Anyone know a free tool to convert .dll into .lib in windows?

You are wrong on two counts. Firstly, DLLs and LIBs (static libraries) are very different beasts. The LIB you are talking about (I think) is the export library, which is simply a list of names in the 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.

To use a DLL with a modern IDE (I don't use VS) you typically include the matching .LIB (export library) in the project. 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.

And secondly, DLLs can be used with C++.

DLL are specific windows executables which load on runtime. Their equivalent in Linux is the *.so .
If you want to understand what's the difference between a DLL and a static lib see here .

Main reason has probably something to do with dll-file being an output of the linker (link.exe). This command line utility doesnt know how to read dlls, only how to write them.

Actually sometimes .lib-files contain more than a list of functions. For example libpng.lib works as a standalone file, without any dll file.

In order to use a DLL in C/C++ you need to link with what is called an import lib . This is a small .lib that contains the names/ordinals the DLL exports in a format that the linker understands. Once this has been linked in, the resulting binary will be able to use the 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.

You can use the following analogy to understand the difference between a dll and a lib file.

the dll is like the source .cpp file while the lib is the header .h file.

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.

It is also possible (in some cases) to generate the lib from the dll . 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. Sending relevant information is then your own problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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