Understanding of dynamic library and static library, static library

Source: Internet
Author: User
Tags types of functions

Understanding of dynamic library and static library, static library

1.1 concept of Database

In my opinion, the Library is already written, mature, reusable code. Every program depends on many underlying libraries. It is impossible for everyone to write code from scratch. Therefore, the existence of the library is of great significance.

In our developed applications, there are usually some public code that need to be used repeatedly, So we compile the code into a library file.

Library can be viewed as a collection of target files, which are compressed and packaged to form a file. On a platform like Windows, the most common C language library is the Runtime Library attached to the integrated development environment. These libraries are generally provided by the compilation vendor.

1.2 create and use static databases in

1.2.1 create a static library

1. Create a new project, select "general" from the installed template, select "Empty Project" under the type on the right, and enter staticlib in the name and solution name. Click OK. 2. Add the mylib. h file in the header file of Solution Explorer, and add the mylib. c file in the source file (that is, the implementation file ). 3. Add the following code to the mylib. h file:
# Ifndef TEST_H # define TEST_H int myadd (int a, int B); # endif
4. Add the following code to the mylib. c file:
# Include "test. h" int myadd (int a, int B) {return a + B ;}
5. Configure project properties. Because this is a static Link Library, select "general" under "configuration properties" of the Project property, and select "static library (. lib)" in the configuration type ). 6. compile and generate a new solution. In the Debug folder, you will get mylib. lib (object file library. the lib file and the corresponding header file are provided to the user, and the user can use the functions in the library.

1.2.2 use of static databases

Method 1: Configure Project Properties
A. Add the header file directory of the project: Project --- properties --- configuration properties --- c/c ++ --- General --- additional include directory: add the header file storage directory. B. Add the lib static LIBRARY PATH referenced by the file: Project --- properties --- configuration properties --- linker --- General --- additional library Directory: add the lib file storage directory. C. Then add the lib file name referenced by the project: Project --- properties --- configuration properties --- linker --- input --- additional dependency: add the lib file name.
Method 2: Use the compiled statement
# Pragma comment (lib, "./mylib. lib ")
Method 3: Add a project
Just like you add. h and. add the lib file to the project file list. switch to "solution View", ---> select the project to add lib --> right-click --> "add" --> "existing items" --> select lib file --> OK.
 

1.2.3 Advantages and Disadvantages of static databases

 
N the link of the static library to the function library is completed during the compilation period, and the static library is copied to the program in the Link stage of the program, which is irrelevant to the program running; the n program is no longer related to the function library during runtime, which facilitates transplantation. N is a waste of space and resources. All related target files and function libraries are linked to form an executable file.
Static connections between memory and disk space are simple and easy to understand in principle. In the early days when operating systems and hardware were not developed, most systems used this solution. With the development of computer software, the disadvantages of this method are quickly exposed, that is, the static link method is very serious for computer memory and disk space waste. Especially in multi-process operating systems, static links greatly waste memory space. In the current linux system, a common program will use the C language static library at least 1 MB, so if there are 2000 such programs on the disk, it will waste nearly 2 GB of disk space. The waste of space for program development and publishing is a problem of static links. Another problem is that static links may cause a lot of trouble to update, deploy, and release programs. For example, the mylib. lib is provided by a third-party vendor when the vendor updates the capacity of mylib. lib, then our program will get the latest version of mylib. lib, re-compile the link, and then release the new program to the user. The disadvantage is obvious: Once the program has any module update, the whole program needs to re-compile the link and publish it to the user. The user needs to re-install the whole program.

1.3 create and use a dynamic library in

To solve the two problems of space waste and updating difficulties, the simplest way is to separate the modules of the program to form independent files, rather than link them together statically. Simply put, it means that the target programs that constitute the program are not linked until the program runs. That is to say, the whole link process is postponed to the runtime, which is the basic idea of dynamic link.

1.3.1 create a dynamic library

1. Create a new project, select "general" from the installed template, select "Empty Project" under the type on the right, and enter mydll in the name and solution name. Click OK. 2. Add the mydll. h file in the header file of Solution Explorer, and add the mydll. c file in the source file (that is, the implementation file ). 3. Add the following code to the test. h file:
# Ifndef TEST_H # define TEST_H _ declspec (dllexport) int myminus (int a, int B); # endif
5. Add the following code to the test. c file:
# Include "test. h" _ declspec (dllexport) int myminus (int a, int B) {return a-B ;}
5. Configure project properties. Because this is a dynamic link library, select "general" under "configuration properties" of the Project property, and select "dynamic library (. dll)" in the configuration type ). 6. compile and generate a new solution. In the Debug folder, you will get the mydll. dll (object file library. dll file ,. the lib file and the corresponding header file are provided to the user, and the user can use the functions in the library.
Question 1: __ what does declspec (dllexport) mean? The dynamic link library defines two types of functions: the export function and the internal function ). Export functions can be called by other modules. Internal functions are used within the DLL program that defines them. Question 2: What is the difference between the lib file of the dynamic library and the lib file of the static library? When using a dynamic library, we often provide two files: one import library (. lib) file (also known as "import library file") and one DLL (. dll) file. Although the suffix of the imported library is "lib", the imported library file and the static library file are essentially different. For a DLL file, the imported library file (. lib) contains the name of the function and variable exported by the DLL, and. the dll file contains the actual functions and data of the DLL. When a dynamic library is used, you only need to link the DLL import file when compiling the executable file. The function code and data in the DLL are not copied to the executable file, it is not until the executable program runs, that the required DLL is loaded, the DLL is mapped to the address space of the process, and then the exported function in the DLL is accessed.
 

1.3.2 use of dynamic libraries

Method 1: implicit call
Create the main program TestDll and copy mydll. h, mydll. dll, and mydll. lib to the source code directory. (P.S: header file Func. h is not required, but must be declared before using external functions in C ++.) Specify the link reference Link Library in the program: # pragma comment (lib ,". /mydll. lib ")
Method 2: explicitly call
HANDLE hDll; // declare a dll instance file HANDLE hDll = LoadLibrary ("mydll. dll "); // import the dynamic link library MYFUNC minus_test; // create a function pointer // get the function pointer minus_test = (MYFUNC) GetProcAddress (hDll," myminus ");

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.