The difference and advantages and disadvantages of static link library and dynamic link library

Source: Internet
Author: User

The difference between a dynamic-link library and a static-link library

This article references the following blogs:

1. http://blog.csdn.net/gamecreating/article/details/55041522. http://blog.csdn.net/left_la/article/details/120985453. http://blog.csdn.net/augusdi/article/details/6460415

Static Connection library is the use of the (Lib) file function code directly linked into the target program, the program is not required to run the other library files; dynamic link is to call the function of the file module (DLL) and the call function in the file location and other information linked to the target program, the program run time from the DLL to find the corresponding function code, and therefore need the corresponding DLL file support.

Both static and dynamic link libraries are shared code, and if you use a static link library, the instructions in Lib are all directly included 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 DLL file with EXE Independent. 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. A dynamic library is a function that needs to be called, and then calls into the stack execution based on the function mapping table. If there are multiple calls to the same function in the DLL file in the current project , the function will only leave a copy when executed. However, if there are multiple calls to the same function in the Lib file, then when executed, the function will leave multiple copies in the execution space of the current program, and a copy is generated from a single invocation.

The static link library and the static link library call rules are generally compared as follows:

1, Static link library (relatively simple):

First, the use of a static link library requires the library developer to provide the. h header file and the. lib file for the build library. The declaration format in the. h header file of the build library is as follows:

The extern "C" function returns the name of the type function (parameter table);

In the calling program's. cpp source code file, the following:

#include ". /lib.h "

#pragma comment (lib, "... Debug//libtest.lib ")//Specify link with Static library

Second, because the static link library contains all instructions in the EXE file generated by the caller. Therefore, if the use of static link library, then there is no "export a function to provide users with" situation, to use it all want! Or you don't have to!

Static link library (LIB)

In vc++6.0 new a static library project called Libtest, and new Lib.h and lib.cpp two files, lib.h and Lib.cpp source code are as follows:

Files: lib.h

#ifndef Lib_h

#define Lib_h

extern "C" int Add (int x,int y); External functions declared as C compilation, join mode

#endif


Files: lib.cpp

#include "Lib.h"

int add (int x,int y)

{return x + y;}

Compile this project to get a. lib file, which is a library of functions that provides the functionality of Add. Once the header file and the. lib file are submitted to the user, the user can use the Add function directly.
The C library functions in the standard Turbo C2.0 (the scanf, printf, memcpy, strcpy, and so on) come from this static library.
Here's a look at how to use this library to work on the new Libcall project in the workspace where the Libtest project is located. The Libcall project contains only one main.cpp file, which demonstrates the method of calling a static link library with the following source code:

#include <stdio.h>

#include ". /lib.h "//Cannot be lost

#pragma comment (lib, "... Debug//libtest.lib ")//Specify to connect with the static library

int main (int argc, char* argv[])

{printf ("2 + 3 =%d", add (2, 3));}

The call to the static link library is that simple, maybe we use it every day, but we don't understand the concept. Code in #pragma comment (lib, "... Debug//libtest.lib ") means that the. obj file generated by this file should be connected with the LibTest.lib

2, State link library:

The use of dynamic link libraries requires the library developer to provide the generated. lib file and the. dll file. or provide only DLL files.

First we have to notice that the functions within the DLL are divided into two types:

1) external functions, which are available for application invocation;

2) DLL intrinsics can only be used in DLL programs and applications cannot invoke them.

So the calling program wants to call a function in the DLL in some form or in a way to indicate which function it wants to invoke.

    • The invocation of a dynamic library function can be statically linked, with the following main steps:

1) contains the exported header file in the DLL.

2) Adopt #pragma comment (lib, ". Debug//libtest.lib ") to import the *.lib header file generated by the dynamic library. Or, add the Lib file to the Projectàsettingsàlinkeràinput additional dependencies.

3) Place the *.dll file generated by the dynamic library in the same directory as the EXE or DLL.

    • It can also be invoked in a dynamically loaded manner, as follows:

Another.dll has an int Add (int x,int y) function. The complete invocation process is as follows:

typedef INT (* funptr) (int,int); Defining function pointers

Funptr funptr;

Handle Handle =loadlibrary ("Another.dll");

Funptr = (funptr) GetProcAddress (handle, "ADD");

Funptr (2,3); 2+3;

FreeLibrary (handle); Releasing a loaded dynamic library

Ii. lib File

There are two kinds of libraries with Lib suffix, one is static link library (static libary, hereinafter referred to as "still Library"), and the other is the import library (import libary, hereinafter referred to as "import library") of Dynamic Connection library (DLL, hereinafter referred to as "Dynamic Library").

A static library is a package of one or more obj files, so some people simply refer to the process of generating lib from the obj file as archive, which is merged together. For example, if you link a static library, if there is a mistake, it will find out exactly which obj is wrong, that is, static Lib is just a shell.

The dynamic library usually has the corresponding import library, the convenience program statically loads the dynamic link library, otherwise you may need oneself loadlibary the DLL file, then GetProcAddress obtains the corresponding function manually. With the import library, you only need to link to the import library and follow the declaration of the header file function interface to call the function.

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 located in the dynamic library, the import library contains only the address symbol table, etc., to ensure that the program to find some basic address information of the corresponding function.

This is also in fact a lot of open source code publishing idiomatic way:

1. Precompiled Development Kit: contains some. dll files and some. lib files. One of the. Lib here is the import library, not the wrong way to think of a static library. However, as with static libraries, the path to find these. Lib is added on the link path. The. dll is best placed in the same directory as the last generated application exe executable file. When this is run, the dynamic link library is automatically transferred.

2, the user compiles themselves: The download is the source code, according to the readme itself compiled. Generate a library file that is most likely also a. dll +. lib (import library)

3, if you only have a DLL, and you know the function of the DLL function prototype, then you can directly in your own program using Loadlibary to call into the DLL file, GetProcAddress get the function address, and then called.

Third, DLL files

A dynamic-link library (DLL) is an executable file that acts as a library of shared functions. Dynamic linking provides a way for a process to invoke a function that is not part of its executable code. The executable code of the function is in a DLL that contains one or more functions that have been compiled, linked, and stored separately from the processes that use them. DLLs also help to share data and resources. Multiple applications can access the contents of a single copy of a DLL in memory at the same time.

Dynamic linking differs from static linking in that it allows executable modules (. dll files or. exe files) to contain only the information required to locate the executable code of the DLL function at run time. In a static link, the linker gets all the referenced functions from the static link library and places the library with the code in the executable file.

There are several advantages to using dynamic links instead of static links. DLL saves memory, reduces switching operations, saves disk space, is easier to upgrade, provides after-sales support, provides mechanisms to extend the MFC library classes, supports multi-language programs, and makes the creation of international versions easy.

1 Advantages of the static link library

(1) The Code loading speed is fast, the execution speed is slightly faster than the dynamic link library;

(2) Just make sure you have the right one on the developer's computer. LIB file, you do not need to consider on the user's computer when you publish the program in binary form. LIB file exists and version problem, can avoid DLL hell and so on.

2 Advantages of the dynamic link library

(1) Save more memory and reduce page switching;

(2) DLL file and EXE file independent, as long as the output interface is unchanged (that is, the name, parameters, return value type and calling convention unchanged), the replacement DLL file will not have any impact on the EXE file, thus greatly improving the maintainability and extensibility;

(3) programs written in different programming languages can call the same DLL function just according to the function calling convention;

(4) Suitable for large-scale software development, make the development process independent, the coupling degree is small, facilitates the development and the test between different developer and the development organization.

3 deficiencies

(1) The executable file generated by using static link is large in size and contains the same common code, resulting in waste;

(2) The application using the dynamic link library is not self-sufficient, it depends on the DLL module to exist, if the use of dynamic link loading, the program started to find that the DLL does not exist, the system will terminate the program and give an error message. While using runtime dynamic linking, the system does not terminate, but because the exported function in the DLL is not available, the program fails to load, and the speed is slower than the static link. When a module is updated, if the new module is incompatible with the old module, then the software that needs the module to run is torn away. This is common in early windows.

The difference and advantages and disadvantages of static link library and dynamic link library

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.