bjskishore123
bjskishore123

Reputation: 6342

Is it possible to link lib dynamically like a DLL?

This is interview question.

Is it possible to link lib dynamically like a DLL ?

For example, for DLL, we use LoadLibrary and call exported functions. Is it possible to use lib file in same manner?

Upvotes: 3

Views: 313

Answers (4)

harper
harper

Reputation: 13690

No. It's not possible. A DLL is module with a PE32 header with all information to load it into a process. A LIB is only a archive of OBJ files.

And despite others say it's easy to wrap a DLL around it, this can be quite difficult. The reason is, that a .LIB not only resolves some dependencies but can also have unresolved externals.

As long as these unresolved externals require only the compiler's runtime library wrapping in a DLL might work. You can check this when you create a DLL project, probably with a minimal C++ source, and try to compile. Than you see, if further externals must be resolved with other libraries.

One important problem may arise with memory management. When yo link statically with a .LIB you will use all the same definitions. If your library comes with own implementation, let's say of malloc-stlye functions, this will not be linked to your application as long as you add all these symbols to the EXPORT list. Finding the list of public symbols that should be included in the EXPORT table may be a pain.

Upvotes: 1

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361402

No. .lib library are statically linked, and that is the purpose they're created for, to resolve the name symbols at link-time by Linker, and link-time occurs before runtime. They're often referred to as "static libraries" (that is why I added this tag in your question!). That is the brief story of lib.

However, you can create a DLL wrapper, if you really want to link at runtime.

Upvotes: 5

JoeG
JoeG

Reputation: 13182

Yes - not directly, but with a very small amount of work.

Create a new .DLL project, link the .lib, define which functions you want to export in a .DEF file then compile.

Upvotes: 0

wilx
wilx

Reputation: 18228

No. Create a DLL instead or, if you do not have the source, wrap the functionality in the .lib with own DLL interface.

Upvotes: 2

Related Questions