user1612986
user1612986

Reputation: 1415

static and dynamic library linking in visual studio c++

in visual studio 2010 i built project as a dynamic library (say dyn1.dll) which is uses another dll (say dyn2.lib, dyn2.dll, dyn2.h are the respective files). i include dyn2.h and dyn2.lib directly in my project. and set the "path" variable to point to the place dyn2.dll resides. this is the procedure i follow to make dyn1.dll work.

now say i build my project as a static library(say sta1.lib) while still using dyn2.dll, dyn2.h and dyn2.lib. is all the code contained within dyn2.lib, dyn2.dll incorporated within sta1.lib.

meaning is sta.lib standalone. do i not need to point to the directory of dyn2.dll in the path variable for sta1.lib to work? i am new to these so a clarifying answer would be very helpful

thanks

Upvotes: 3

Views: 4615

Answers (1)

WhozCraig
WhozCraig

Reputation: 66254

The simple answer is no. Code from dyn2.dll is not included in your static dyn1.lib (sta1. lib). In fact you will have to link dyn2.lib with any project that now uses your static sta1.lib or you will have unresolved linker errors.

This is not to say your static library isn't referencing code in dyn2.dll; it is. But being a static library, there is no PE module. It is just a collection of .obj code modules with external references. Those external references (to dyn2.dll) still need resolving by whomever uses code from your static library that, in turn, uses code references to dyn2.dll. There is no "link-time" resolution for static libraries. That only happens when you link your final PE (be it a DLL or a EXE). Things still need to be fixed up at final link.

Therefore, to answer your second question. In either case (static or dynamic), your final executable still needs to have load-access to dyn2.dll, wherever it may be. Furthermore, it must also now link with the dyn2.dll import library (dyn2.lib) just as your dyn1.dll had to prior.

Upvotes: 3

Related Questions