When to include .lib and when to include .dll or both

I have a .h file, two .lib files, a DLL file and a tiny test project from a hardware vendor to talk to my hardware.

Compiling and running their test project works fine. Remarkably: they do not use DLLs. I can drop the dll directory and all its contents, everything works fine.

To get started, I just copied parts of my code (connect, disconnect, and send a command) to my project. In fact, that’s all you can do. I included the .h file and pointed to the directory containing the .lib files. Like a tiny test project. All this compiles, but when I try to start the project, it complains that it is missing a DLL file.

Can anyone explain what is happening? How should the lib and dll libraries work?

All this in Windows VS2005. I was comparing .vcproj files and could not find any significant differences.

+4
source share
2 answers

The test project is statically linked - lib is included in exe.

Your project is dynamically linked - a link to the dll is provided and therefore needed at runtime.

See this stack overflow question for more information.

+2
source

Basically, the answer depends on whether you will use static or dynamic snapping for your executable.

With static binding, you need .h and .lib files, but not .dll files for compilation and linking. Your executable will be larger, but at runtime you will not need .h / .lib / .dll files.

With dynamic linking, you just need .h files to compile and link. Your executable will be smaller, but at runtime you will need one or both of the DLL files.

For a more detailed discussion of this from the point of view of Visual Studio, http://msdn.microsoft.com/en-us/library/1ez7dh12.aspx -

"Dynamic linking differs from static linking in that it allows the executable module (either a DLL file or an .exe file) to include only the information needed at run time to find the executable code for the DLL function. With a static link, the linker gets all the link functions from library of static links and puts it with your code in your executable file. "

+2
source

Source: https://habr.com/ru/post/1299419/


All Articles