Lib and dll difference generates and uses detailed (rpm)

Original article addresses: https://www.cnblogs.com/TenosDoIt/p/3203137.html#c

 

First, some (static link library), the concept of a static library DLL (dynamic link library), the first code-sharing both ways.

Static library : In the link step, the connector from the library files needed to obtain the code, copy the resulting executable file (copy the link process has been good) in this library is called a static library, which is characterized by exe It contains a complete copy of the library code; drawback is that it will be used more than once have multiple redundant copies. That static library instruction have all been directly included in the final generated EXE file in the. In the New vs generate static library project, after successfully compiled, produces only a .lib file

DLL : The dynamic link library that contains by multiple programs at the same time using the library code and data, DLL is not an executable file. Dynamic Link (This is the origin of its name) provides a way to make the process does not belong to its function can be called executable code (such as when exe call a static library function, this part of the code has been copied to in the exe). Function executable code in a DLL, the DLL comprising one or more has been compiled, and linked with the process using them separately stored function. In the New vs generate dynamic library project, compiled successfully, generate a .lib file and a .dll file

Then the above static and dynamic libraries in the lib What difference does it make?

Static library lib : The LIB contains a function code itself (including an index function implemented +), was added directly to the program code which when compiled

Dynamic library lib : The LIB contains information (index) DLL files and functions are located in the function of the location, function implementation code loaded in the process space provided by the runtime DLL

In short, lib is used at compile time, dll is used at runtime. To complete source code to compile, just lib; if you want dynamically linked programs up and running, only DLL .

The following examples are tested on vs2010


Generating and using dynamic libraries

Build a dynamic library

 New Project --win32 Project - Fill in the project name - OK - the next step - Application Type: Select dll-- additional options: Select Export Symbols - complete

You can see generated a dllmain.cpp file, which is the entry dll application , pay attention to it and the entrance main function of different engineering general, we do not need to modify this file.

 In this example we export dynamic library in a variable, a class, a function, the header file dll.h as follows:

 1  // When creating a new generation of dll project, vs default defines the macro DLL_EXPORT, therefore, DLL_API is __declspec (dllexport), used to derive 
 2  // When we call a static dll (implicitly called), we included the head files, because there is no definition of DLL_EXPORT, it is DLL_API 
 . 3  // __declspec (dllimport), for introducing 
 . 4 #ifdef DLL_EXPORTS
  . 5  #define DLL_API __declspec (dllexport)
  . 6  #else 
 . 7  #define DLL_API __declspec (dllimport)
  . 8  #endif 
 . 9  
10  / / derived class 
. 11  class DLL_API {Cdll
 12 is  public :
 13 is      Cdll ( void );
 14      // the TODO: Add your this method.
15 };
 16  
. 17  // derived variable, the variable is defined in the .cpp file 
18 is  extern DLL_API int nd11;
 . 19  
20 is  // export functions, plus extern "C", is generated in order to ensure the same function name at compile time, so that dynamic when call to dll 
21  // correct function to obtain the address of 
22  extern  " C " DLL_API int fndll ( void );

TODO: The following content has to be finishing ~

 

Guess you like

Origin www.cnblogs.com/Stephen-Qin/p/11369371.html