Create and use Windows static and dynamic libraries

Source: Internet
Author: User
Tags import database

 

In actual programming and development, we often encounter that a DLL file or link cannot be found at runtime. Then, we will start to mess up Google, and then change the vs2005 settings. Maybe it will be OK. We will import the DLL or lib developed by others into our programming, so what are these lib and DLL? Below, I will explain it in detail.

 

First, let's say the first one.:Static link library (Static libary)

We use vs2005 to create a static Link Library first.

Open vs2005 and create a project (staticcai) named Win32 console application.

 

Create static_lib.h and static_lib.cpp. The content of these two files is as follows:

 

 

 

Static_lib.h:

 

Int add (int x, int y );

Int substract (int x, int y );

 

Static_lib.cpp:

 

# Include "static_lib.h"

 

Int add (int x, int y)

{

Return X + Y;

}

 

Int substract (int x, int y)

{

Return x-y;

}

Then compile and generate the solution. In this case, a staticcai. Lib file is generated in the debug folder (in parallel with staticcai). This is our static Link Library. Next, let's see how to use this static Link Library. Create a Win32 console program and create main. cpp as follows:

# Include <iostream>

# Include "static_lib.h"

# Pragma comment (Lib, "static. lib ")

Using namespace STD;

 

Int main ()

{

Cout <add (3, 4) <Endl;

Cout <substract (5, 3) <Endl;

Return 0;

}

Copy the staticcai. lib and static_lib.h files to the same folder as main. cpp. Then, we compile, link, and execute the program, and the result will be displayed.

# Pragma comment (Lib, "static. lib") has the same effect as adding dependencies in project à property à connector. So far, how to do static Link Library and how to use static Link Library is done. Now, we have deleted the staticcai. Lib we just copied. We found that the program is still executed, but it cannot be linked any more. Therefore, we come to the conclusion that we need a static link library when linking again. Once the link is successful and an executable file is generated, the static Link Library is no longer needed.

Secondly, let's say the second one.:Dynamic Link Library (Dynamic Link libary)

Similarly, we will create a dynamic link library. Like the above steps, we will first build a project, and only the last step is slightly different.

 

Then, create the DLL. cpp file (we will not do the. h file here) and enter the following content:

# Define dll_api _ declspec (dllexport)

# Include <iostream>

Using namespace STD;

Dll_api int add (int A, int B) // Add two integers

{

Return A + B;

}

Dll_api int subtract (int A, int B) // subtract two integers

{

Return A-B;

}

Then, we compile and generate the solution, and the dllcai. dll and dllcai. Lib will be generated in the debug folder. Now, the dynamic link library is ready. Let's see how to use it. Create a Win32 console program and create main. cpp as follows:

# Include <iostream>

Using namespace STD;

# Pragma comment (Lib, "DLL. lib ")

Extern int add (int A, int B );

Extern int subtract (int A, int B );

Int main ()

{

Cout <add (3, 4) <Endl;

Cout <subtract (5, 3) <Endl;

Return 0;

}

Copy dllcai. dll and dllcai. lib to the directory that is parallel to main. cpp. Next, compile, link, and execute the command, and you will see the same result as the previous one. Then, we set the dllcai. after Lib is deleted, the program is still executed, but the link is no longer available. Next, we will use dllcai. after the DLL is deleted, the program can be compiled and linked.

 

 

Therefore, we say: for dynamic link libraries, the. Lib file is required for linking, And the. dll file is required for running.

 

So far, we have finished talking about static and dynamic link libraries. Let's make a comparison and supplement:

1. Two lib files

We found that both static and dynamic libraries have lib files. What is the difference between the two? Actually, two are completely different. The staticcai. Lib size is 4 kb, The dllcai. Lib size is 2 kb, the Lib file corresponding to the static library is called the static library, and the Lib file corresponding to the dynamic library is called the imported library. In fact, the static library itself contains the actual execution code, symbol table, and so on. For the import and export operations, the actual execution code is located in the dynamic library, the imported database only contains the address symbol table. Make sure that the program finds some basic address information of the corresponding function.

2. for static link libraries, all the code has been imported during compilation and linking. Therefore, after an executable file is generated, the executable file contains all the code. Therefore, the static library is no longer needed when the executable file is run, which is why we delete staticcai. the Lib program is still executed. For dynamic link libraries, the executable file does not contain the content in the DLL, but is imported to the database through the import (. lib) knows the corresponding address information. Therefore, the executable file must dynamically load the DLL at runtime, which is why we delete dllcai. after the DLL, the program cannot be executed.

3. For DLL, we can avoid lib files.

If the Lib file is not required, we can use the function pointer to achieve our goal:

# Define dll_api _ declspec (dllexport)

# Include <iostream>

Using namespace STD; // note that extern "C" must be added here.

Extern "C" dll_api int add (int A, int B) // Add two integers

{

Return A + B;

}

Extern "C" dll_api int subtract (int A, int B) // subtract two integers

{

Return A-B;

}

# Include <iostream>

# Include <windows. h>

Using namespace STD;

 

Typedef int (* func) (int x, int y); // function pointer

Int main ()

{

Hinstance = loadlibrary ("DLL. dll ");

If (hinstance = NULL)

{

Cout <"SB" <Endl;

Return 0;

}

Func add = (func) getprocaddress (hinstance, "add ");

Func sub = (func) getprocaddress (hinstance, "Subtract ");

Cout <(* Add) (3, 4) <Endl;

Cout <(* sub) (5, 3) <Endl;

}

Obviously, this method is not convenient with the Lib file. It is a lot of trouble to get another function pointer every time you call a function. Therefore, in actual development, many third-party extension libraries are provided by others:

. H file (class, function declaration)

. Dll file (implementation of classes or functions)

. Lib file (import to database)


Summary:

I. Static Library

* The static library is used to compile the functions required for running the program in a binary file with the extension. Lib. When the program is linked, the binary data in the static library is put together with other program data. The support for lib and DLL files is not required when the program is running. The disadvantage is that the developed program occupies a large disk space. In particular, in Windows systems, functions that are originally required for running programs do not have to use their own static libraries each time a program is developed.

* The static library is in the form of a. Lib file.

* The executable file generated after the Link contains the code of all functions to be called, so it occupies a large disk space.

* If multiple (calling the same library function) processes run in the middle of the memory, multiple identical library function codes exist in the memory, so the memory space is occupied.

Ii. Dynamic library

* During development, the dynamic library only puts the function names and parameters in the DLL into the application. When the application is running, it calls the functions in the DLL according to the function name and parameters to run the function, in this way, the application in the operating system can use the same DLL at the same time. This can effectively save hard disk space. Of course, this makes program design more hierarchical. It is also conducive to the division of labor and information security of software engineers.

* A dynamic library exists in the form of a. DL file, and generally a corresponding import database exists in the form of a. Lib file. Pure resource DLL does not generate. Lib import to the database.

> The extensions of both the import and import libraries and static libraries are *. lib, but the import library only contains some function name and parameter information. Without the function body, it is used to call the dynamic library service. It has the same relationship with the dynamic library. h file and. relations between CPP files;

* Two dynamic library binding methods

> Static blnding: when a program that uses static binding loads memory at the beginning, it calculates and determines the address of all the dynamic code called by the program. In this way, the initialization time of the program is long, but once dynamic loading is completed, the program runs quickly.

2. dynamic binding a program using this method does not complete the dynamic link at the beginning, but does not wait until the dynamic library code is actually called, load the program to calculate the logic address of the dynamic code (the called part). Then, when the program needs to call another dynamic code, load the program to calculate the logic address of this part of the code. Therefore, in this way, the initialization time of the ghost program is short, but the performance during running is inferior to that of the static bound program.

* Two methods for using dynamic libraries (Windows)

> Method 1: Load-Time Dynamic Linking
When you want to call the DLL application link, include the DLL input library file (import library,. Lib file. The specific method is to add # include at the beginning of the source file, and then you can call the output file in dlldemo. dll in the source file.

> Method 2: Run-Time Dynamic Linking
Instead of including the input library file during the link, you can use loadlibrary or LoadLibraryEx to dynamically load the DLL in the source program.

 

Related Article

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.