The difference between static link and dynamic Link (reprint)

Source: Internet
Author: User

1. Reprint: http://www.cnblogs.com/kex1n/archive/2011/09/06/2168435.html

Dynamic link library, static library, import library differences

Dynamic link libraries (Linked library):
Windows provides rich function calls to applications that are included in the dynamic-link library. There are 3 of the most important dll,kernel32.dll, which contain functions for managing memory, processes, and threads;

User32.dll, which contains functions for performing user-interface tasks such as window creation and message delivery; GDI32.dll, which contains functions for drawing and displaying text.

Static Library:
Functions and data are compiled into a binary file (usually with an extension of. LIB). In the case of a static library, when compiling a linked executable, the linker copies the functions and data from the library and puts them and other modules of the application

Combine to create the final executable file (. EXE file).

Import Library:
When using a dynamic link library, there are often two files available: An introduction library and a DLL. The introduction library contains the symbolic names of the functions and variables exported by the DLL, and the DLLs contain the actual functions and data. When compiling a linked executable file, simply

To link to the library, the function code and data in the DLL are not copied to the executable file, and at run time, the DLL is loaded, and the exported functions in the DLL are accessed.

When you run a Windows program, it touches windows through a process called dynamic linking. A Windows. exe file has a reference to it using a different dynamic-link library, where the function is used. When Windows thread

When the sequence is loaded into memory, the call in the program is directed to the entry of the DLL function, and if the DLL is not in memory, the system loads it into memory.

When you link a Windows program to produce an executable file, you must link the Specialized import library library that is provided by the programming environment. These import libraries contain the dynamic link library name and all Windows function calls.

Reference information. The linker uses this information in the. EXE file to construct a table when the loader is loaded, Windows uses it to convert calls to Windows functions.

The difference between a static library and an import library:
The difference between an import library and a static library is that they are essentially different things. The static library itself contains the actual execution code, symbol table, and so on, and for the import library, its actual execution code is in the dynamic library, the import library contains only

Address symbol table, etc., ensure that the program finds some basic address information for the corresponding function.


Static links and Dynamic Links:

Static linking method: #pragma comment (lib, "Test.lib"), static link, loading code will be used by the program's dynamic code or the address of the dynamic code to determine down
Links to static libraries can use static links, and dynamic-link libraries can also use this method to link import libraries

Dynamic linking methods: LoadLibrary ()/getprocessaddress () and FreeLibrary (), programs that use this method do not complete dynamic links in the first place, but until the dynamic library code is actually called, the loader computes (the part that is called) The logical address of the dynamic code, and then at some point, the program needs to call another block of dynamic code, loading the program to calculate the logical address of this part of the code, so this way to make the program initialization time is short, but the performance of the run compared to statically linked programs.

In the process of software development, we often use a dynamic library or a static library written by others or provided by the system, but do you use static or dynamic libraries? What are their conditions of application?

Simply put, static libraries and applications are compiled together and run in any case, while dynamic libraries are dynamic links, as the name implies when the application is launched, so the application fails when the dynamic library is not available on the user's system. Then look at their features:

Dynamic libraries:

1. Sharing: Multiple applications can use the same dynamic library, when launching multiple applications, only need to load the dynamic library into memory once;

2. Development module Good: requires the designer to partition the function better.

Static Library: The code is loaded faster and executes faster, because it will only link the part you need when compiling, and the application is relatively large. However, if multiple applications are used, they will be loaded multiple times, wasting memory.

On the whole, I personally think that if you have more than one application on your system to use the library, it is compiled into a dynamic library, so that although the start of the load is relatively slow, but multi-task time will be compared to save memory; If only one to two apps on your system use the library and use less APIs, Compiled into a static library, the general Static library can also be clipped compiled, so that the application may be larger, but the speed of the startup will be greatly improved.

2. Reprint: http://blog.csdn.net/gamecreating/article/details/5504152

Another difference between a static-link library and a dynamic-link library is that the static-link library can no longer contain other dynamic-link libraries or static libraries, and other dynamic or static link libraries may be included in the dynamic-link library.

3. Reprint: http://www.cnblogs.com/tracylee/archive/2012/10/15/2723816.html

first, compile and link separately (linking )

Most high-level languages support separate compilation, and programmers can explicitly divide the program into separate modules or files, and then compile each individual part separately. After compilation, these separate fragments (called compilation units) are "glued together" by the linker. (think of the benefits of doing this?) )

These standalone compilation units include the Obj file (which is compiled by the generic source program), the Lib file (a statically linked function library), the DLL file (a dynamically linked function library), and so on.

Static link mode: Complete all assembly work before the program executes, generating an executable target file (exe file).

Dynamic Link mode: The program has completed linking work for execution after it has been loaded into memory, and generally retains only one copy of the compilation unit in memory.

Second, static link library and dynamic link library

To illustrate the concept of DLL (Dynamic linkable Library), you can simply think of a DLL as a repository, which provides you with variables, functions, or classes that you can use directly.

Both static and dynamic link libraries are shared code, and if you use a static link library, the instructions in Lib are included directly in the resulting EXE file, whether you want to or not. However, if you use a DLL, the DLL does not have to be included in the final EXE file, exe file can be "dynamically" to reference and unload the EXE independent DLL file.

The advantages of using the dynamic link library: (1) More memory saving, (2) DLL file and EXE file independent, as long as the output interface is not changed, the replacement DLL file will not have any impact on the EXE file, thus greatly improving the maintainability and extensibility.

Iii. knowledge of dynamic link library

A dynamic link is relative to a static link. A static link is a link to a function or procedure to be called into an executable file, as part of an executable file. In other words, the code for the function and procedure is in the EXE file of the program, which contains all the code required by the runtime. When multiple programs call the same function, multiple copies of the function are present in memory, wasting valuable memory resources. The function code called by the dynamic link is not copied to the application's executable file, but is simply added to the description of the called function (which is often the relocation information). The link relationship between the application and the corresponding DLL is established only when the application is loaded into memory and starts running under Windows Management. When you execute a function in the called DLL, Windows goes to execute the corresponding function code in the DLL based on the relocation information generated by the link. In general, if an application uses a dynamic-link library, the WIN32 system guarantees that only one copy of the DLL is in memory

Two ways to link a dynamic link library:

(1) Load-time dynamic link (load-time linking): This usage is based on the premise that you know which functions to invoke in the DLL before compiling, and that only the necessary link information is kept in the target file at compile time, without the code of the DLL function, when the program executes, The main purpose of the function is to facilitate code sharing by using the link information to load the DLL function code and to link it to the execution space of the calling program in memory (all functions are loaded into memory). (dynamic loader, in the loading phase, mainly to share code, share code memory)

(2) Runtime dynamic Link (Run-time linking): This means that you do not know before compiling which DLL functions will be called, it is entirely in the run process as necessary to decide which function should be called, load it into memory (only the function that is called into memory), and identify the memory address, other programs can also use the program, and LoadLibrary and GetProcAddress dynamically get the DLL function entry address. (DLL only has one copy in memory, at run time)

The difference is mainly in the stage, whether the compiler knows the DLL function to be called by the process. Dynamic loading knows what functions are called at compile time, but must not be known in the run state.

The difference between static link and dynamic Link (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.