Static and dynamic linking in Visual Studio

I understand the concept of static and dynamic binding. It is known that there are dynamic libraries on the Windows .dll platform, and .lib are static libraries.

My confusion: I created a project in which I had to use the OpenCV libraries. Basically, I had to use the following 5 OpenCV libraries:

 -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_videoio 

For this purpose, in the project properties, I had to specify the compiler path to the libraries in the Additional Library Directory VS 2012, and I also had to tell the linker about the .lib libraries that I want to use for the project. The project was compiled without errors. But when I try to start the project, he said that videoio.dll missing (same error for other libraries). As soon as I copied the .dll files inside the folder where the .exe present, the project went fine.

Question: Why did I have to copy .dll files when I already linked the static libraries ( .lib )?

Further question: When I use Eclipse on Mac OS or Linux, I just need to report this, where the OpenCV libraries are present, and the linker that I want to use in other OpenCV libraries. In this case, I never had to put dynamic libraries in the .exe folder.

+5
source share
1 answer

The "regular" Windows toolchains offer two .lib "flavors."

One of them is the static libraries that you mention. When used, there is no related DLL.

The other is a shared library, which only has a hook in the code to load and fix function pointers for the dynamic library at boot time.

With dynamic libraries (.dll), you can load it yourself (via. LoadLibrary ), or you can use the stub file (import) .lib that you provided the .dll with. Thank you for importing .lib, if one is provided.

Why did I have to copy DLL files when I already linked static libraries (.lib)?

The .dll must be in the path or directory with the .exe for the bootloader to find it.


How to distinguish a .lib file from a static library or a dynamic library?

As a rule, the documentation should contain some information about this. If they are concentrated in one folder, I would assume that .lib is tied to .dll. If all else fails, review the file size; if .lib is tied to .dll, then it is usually small compared to .dll.

+8
source

All Articles