Static library VC environment (lib) and using dynamic libraries (Dll) usage (clear version)

Static library using VC environment

A .   Static library includes .lib and .h files , use a static library project is divided into 3 steps:

<1>  added to a static library in the project, there are three ways:

      ** method: the project settings referenced .lib, project-> setting-> link-> object / library modules added .LIB; (specify the path where the input may project> settings> link> Additional library of the path input path .lib files, can also project-> settting and Tools-> Options-> Directories specified path, see specific difference <2>)

      ** Method two: in the project directly into the lib, project-> add to project-> files, select the correct .lib.

      ** Method three: pragma comment (lib, "## / ## / mine.lib"), the path name, the system will first search path environment

 

 

 

<2>  includes a .h file in the project;

Use in your application project

#include "file path"

* file path can be an absolute path, or may be relative to the project (ie .dsp file) where the relative path to the directory. If more file header, can project> settings> c / c ++ > Additional include directories preprocessor you fill in mesh headers is located, or in VC6.0 inside, is disposed in the IDE Tools => Options => Directories in set (^ _ ^, this place can be seen from project-> settting differences and relations with the Tools-> Options-> Directories of the former for the current project; the latter is for the entire vc development environment that once in Tools -> Options-> Directories carried out after a setting, simply open later vc environment, it is still this set !!)

 

 

<3>  using a static library function in the project;

 

////////////////////////////////Dividing line//////////////// ///////////////////////////

 

DLL using VC environment

Second, dynamic link libraries typically include .lib (import library) ,. h, .dll files, dynamic libraries, there are two cases:

A implicit link:. Load-time dynamic linking

With the use of a static library similar, divided into three steps: import library .lib reference (this time the Dll and lib file suffix should be put to), include the header files (h suffix files), use the Export function;

The advantage of this approach is that: can be used like a static library as a direct call function

Disadvantages: loaded when the program starts all the required DLL, take longer to start, low efficiency.

Reasons for the success of this approach are: import library lib contains the location dll file export function, so when you call a function, the function body to know to perform the function of the dll.

B. Dynamic load: run-time dynamic linking

Direct use of dynamic libraries or LoadLibraryEx LoadLibrary load the required (does not require a corresponding header files .h, and LIB), and then specify exported functions required for maximum efficiency! , But only need to have a more detailed understanding of the DLL library.

Disadvantages: Use GetProcessAddress () function to get a pointer, the function call relatively cumbersome.

 Reasons for the success of this approach are: forced through LoadLibrary dll file is loaded into memory and function to obtain the address of the function you want to call by GetProcessAddress (), and then to perform the function body of the function dll file via this address.

 ////////////////////////////////////////////////// /////////// dividing line ///////////////////////////////////// /////////////////

So far, it has been used correctly!

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

Third, the following are all using notes, you can not see, just to help understand.

1. Basic concepts:

Target library (static library):
  extension .lib, static connection, its code will be added to the executable program.
Dynamic Library:
  extension .dll, dynamic linking, occurs at runtime.
Enter the library (import library):
  extension .lib, a special form of object libraries. Enter the library does not contain the code, but to provide information to the linker to use to establish dynamic links relocation table in the .exe file.
  Enter the library is to assist library dynamic library, use the library, the library needs to include the header file in the file referenced in the library vc implicitly import dynamic library when connected and allow the program to find the location for the repository (in the same directory or disposed accordingly in vc).
  Target libraries and libraries of input during the development process, the dynamic library at runtime.
  Generally, VB when introduced into the dynamic libraries by explicitly import (LoadLibrary), vc implicit introduced simpler.

2. State of different lib

There are currently lib suffix to the library two, one for static link library (Static Libary, hereinafter referred to as "static library"), the other is a dynamic link library (DLL, hereinafter referred to as "DLL") import library (Import Libary, hereinafter referred to as "import library"). 
Static library is packaged with one or more obj files, so the process has simply generated from the obj lib file called Archive, that is merged together. For example, you link a static library, if one is wrong, it will find exactly what obj is wrong, that is just a static lib shell. 
Dynamic libraries generally have corresponding import library, easy to program static load dynamic link library (ie import library lib is also linked to the exe file to go), or you may need to call in their own LoadLibary DLL file, and then manually get GetProcAddress It corresponds to the function. With import library, you only need to link the import library according to a statement calling the function header file function interface on it. 
The difference between import and static libraries are great, they are the essence of different things. Static library itself contains the actual code execution, the symbol table, etc., and for import library, which is located in the actual dynamic library code execution, address import library contains only the symbol table, to ensure that the basic function program to find the corresponding address information; the same point: are linked to the exe file.

3 customary manner open source release:

(1) pre-compiler development package: contains some .dll files and some of the .lib file. Here is where the .lib import library instead mistaken for a static library. But the introduction of methods and static libraries, these .lib find the path you want to add in the link path . The .dll is best placed in the same application exe executable file directory generated last . Such is running, it will automatically transferred to the dynamic link library.

(2) the user's own compilation: download the source code, compiled in accordance with its own readme. It may also generate a .dll + .lib (import library) library files

(3) If you only dll, and you know the dll function prototype function, then you can use directly in their own procedures LoadLibary transferred DLL file, GetProcAddress

4. Introduction of some imported function:

LoadLibrary

 General is dynamically loaded DLL (you do not need the corresponding header files and LIB), display load the dll function, provided you own dll exported functions for argument very understanding.
#Pragma the Comment

Generally static loading DLL (corresponding header files, DLL, LIB and indispensable, and the production of EXE DLL file is not found it will lead to "Application failed to initialize")
#import

Import dll is established dll com, com mainly used to resolve the formation of the internal structure, they identify with c ++ call 

 

Guess you like

Origin blog.csdn.net/Qsir/article/details/88287592
Recommended