Sidar
Sidar

Reputation: 544

C++: Static and Dynamic libraries ( compiling and running)

I'm working on a library project which is pretty much finished. So I'm giving it a test run now. I have no problem running my test project. However I'm not entirely sure if I'm doing it right. Mainly because I don't really understand what is happening exactly( at least im not entirely sure).

I've compiled both the DLL and Static library(.a). For my test project i'm using the Headers from my library and linking against the static library. I'm able to compile. But then It seems that I also need the DLL where the executable resides in order for it to run.

So to my understanding, I compile using the static library but I don't exactly embed it into my executable, therefor at runtime it's looking for the DLL?

My confusion comes from the fact that I read that static libraries are usually embedded into the executable. But doesn't that only happen if you specify that in the compiler options?

I'm rather confused about the relationship in my sittuation. Can someone clarify this?

Edit: I'm using GCC. Codelite as my IDE.

Upvotes: 1

Views: 1302

Answers (1)

yuklai
yuklai

Reputation: 1655

lib is static (cannot be dynamically linked at run time) at compile time. So you are correct that the lib is "embedded" in the executable. More precisely, the lib is linked to other object files that the compiler has produced to build the exe file. A lib cannot link to another lib, only an exe or a dll can link to a lib.

dll is dynamically linked by the exe while the exe is run. dll is like another exe, but its entry function is "dllmain" instead of "main". dll can be built with lib's just like exe. dll can also link to other dll's at runtime to interface with these dlls' functionalities. The interface to dll is defined by a def file.

As to why your project would need the dll, you might want to check the calls of LoadLibrary in your project.

Upvotes: 1

Related Questions