Monday 28 November 2011

What is dynamic linking? in C programming

What is dynamic linking?

All Windows programs communicate with the Windows kernel through a process known as dynamic  linking. Normally, with DOS programs, your programs are linked statically. This means that your linker resolves all unresolved external function calls by pulling in the necessary object code modules (.OBJs) to form an executable file (.EXE) that contains the executable code for all functions called within your program. The Windows environment, on the other hand, provides too many functions to be linked statically into one executable program. A statically linked program under Windows would probably be several megabytes in size and horribly inefficient. Instead, Windows makes extensive use of dynamic link libraries. Dynamic link libraries (.DLLs) are somewhat like the C libraries you create under DOS, with the exception that DLLs can be loaded dynamically at runtime and do not have to be linked in statically at link time. This method has several advantages. First, your Windows executables are typically small, relying on calls to DLLs to provide runtime support. Second, Windows can load and discard DLLs on demand—which allows Windows to finetune its environment at runtime. Windows can make room for more programs if it can dynamically discard
functions that are not being used currently.

How does dynamic linking work? It is not an easy process by any means. First of all, when you link your Windows program, your compiler creates a table in your executable file that contains the name of the dynamic link library referenced and an ordinal number or name that represents that function in that dynamic link library. At runtime, when you reference a function call that is located in a dynamic link library, that DLL is loaded into memory, and the function’s entry point is retrieved from your executable’s DLL table. When the DLL is no longer needed, it is unloaded to make room for other programs.

Dynamic link libraries are typically used for large programs with many functions. You can create a DLL with your compiler—see your compiler’s documentation for specific instructions on how to carry out this task.

Cross Reference:

None.

No comments:

Post a Comment