Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static and Dynamic linking in Visual studio

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

My Confusion: I made a project in which I had to use the OpenCV libraries. Basically, I had to use the following 5 libraries of OpenCV:

-lopencv_core
-lopencv_highgui
-lopencv_imgcodecs
-lopencv_imgproc
-lopencv_videoio

For this purpose, in the properties of the project, I had to tell the compiler the path of the libraries in the Additional Library Directory of VS 2012 and I also had to tell linker about the .lib libraries which I want to use for the project. The project got compiled without any error. But when I try to ran the project, it said that videoio.dll is missing (same error for rest of the libraries too). As soon as I copied the .dll files inside the folder where the .exe was present, the project ran fine.

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

Further Question: When I use Eclipse on Mac OS or Linux, I just have to tell the complier where the OpenCV libraries are present and to the linker which other OpenCV libraries I want to use. I never had to put the dynamic libraries to the .exe folder in that case.

like image 263
user2756695 Avatar asked Sep 14 '15 10:09

user2756695


1 Answers

The "usual" windows tool chains offer two "flavours" of .lib.

One is the static libraries you mention. When used, there is no associated .dll.

The other is a stripped library that is only there to hook in the code to load and fix functions pointers for the dynamic library at load time.

With dynamic libraries (the .dll) you can either load it your self (via. LoadLibrary) or you can use the stub (import) .lib you have been provided with the .dll. Favour the import .lib if one is provided.

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

The .dll needs to be in the path, or the directory with the .exe for the loader to find it.


How do I differentiate whether the .lib file is Static library or Dynamic library?

Generally the documentation should include some level of detail about this. If they are lumped in the same folder, then I would assume the .lib is tied to the .dll. If all else fails, look at the file size; if the .lib is tied to the .dll, then it is normally small in comparison to the .dll.

like image 158
Niall Avatar answered Sep 24 '22 14:09

Niall