windows dynamic and static libraries VS import

1. Static and dynamic libraries

1.1 static library (.lib)

Functions and data is compiled into a binary file (usually extension .LIB). In the case of static library, when compiled and linked executable file, the linker functions and data copied from the library and put them to other modules and applications combined to create the final executable (.EXE file). When the product is released only need to release the executable file does not need to publish static library is used.

1.2 dynamic link library (.lib files and .dll files)

When using dynamic libraries often provide two files: an import library (.lib) file (also known as "import libraries") and a DLL (.dll) file. Although the introduction of library extension is "lib", however, the dynamic library import library files and static library files are essentially different, for a DLL file, its import library file (.lib) contains the function of the DLL and exported symbolic name of the variable, and the DLL .dll file contains the actual data and functions. In the case of using shared libraries when compiled and linked executable files, library files only need to link the introduction of the DLL, the DLL function code and data can be copied to the executable file is not until the executable runs, only to load the required DLL, the DLL is mapped into the process's address space, and then access the exported DLL function. In this case, at the time of publishing products, in addition to publishing an executable file, but also you need to publish dynamic link library that the program will be called.

You can create a Windows platform independent program modules into smaller DLL (Dynamic Linkable Library) files, and compile and test them individually. At runtime, only if the case EXE program you really want to call these DLL modules, the system will load them into memory space. This approach not only reduces the size of the memory space requirements and EXE files, but these DLL modules can be programmed to use multiple applications simultaneously. Windows will own some of the major system functions implemented as a DLL module. Windows provides a wealth of function calls for the application of these function calls are included in the dynamic link library. Including three of the most important DLL:

Kernel32.dll, which contains various functions for memory management, process and thread;

User32.dll, each function comprising a user interface for performing tasks (e.g., create and transmit a message window); and

GDI32.dll, which contains various functions for drawing and displaying text.

When Windows .exe program is loaded into memory, the program calls the entry point to the DLL function is, if the DLL is not in memory, the system will be loaded into memory. When the link to the Windows program to produce an executable file, you must link provided by the programming environment of the special "import library (import library)". The import library contains a reference to the dynamic link library names and information for all Windows function calls. Linker uses this information to construct a table in the .EXE file when the program is loaded, it calls into Windows using Windows functions.

1.3 introduces the difference and static libraries

 Introduction and static libraries are very different, their essence is not the same thing. Static library itself contains the actual code execution, the symbol table and the like, while for the introduction of the library, which is located in the actual dynamic library code execution, the import library only contains the address of the symbol table and so on , to ensure that the basic function program to find the corresponding Address information. But the introduction of libraries manner of introduction and static libraries, these .lib find the path you want to add in the link path.

2. VS link lib file

Way to add lib file in VS, there are two:

1) designated by the header file after pragma program.

#pragma comment(lib,"../lib/libmysql.lib")

2) VS specified directory and library name.

2.1 VS import libraries and header files

VS added lib library and header files (external dependencies) the steps of:

1) Add the header file directory of the project: Project properties --- --- Configuration Properties --- c / c ++ --- General --- Additional include directories: add the header file storage directory.

2) add file references a static library lib path: Project properties --- --- --- Configuration Properties Linker --- --- Additional conventional library catalog: plus lib file storage directory.

Then add a project reference to the lib file name: Project properties --- --- --- Configuration Properties Linker Input --- --- Additional Dependencies: add lib file name.

3) add a project reference to the dynamic library dll: the directory referenced dll project into an executable file.

3. VS Runtime difference between MT and MD

VS Project Properties -> Configuration Properties → C / C ++ → code generation → runtime library can be used are: multi-threaded (/ MT), multi-threaded debugging (/ MTd), multi-threaded DLL (/ MD), multi-threaded debugging DLL (/ MDd).

/ MT is "multithread, static version", meaning multi-threaded static version, after it is defined, the compiler LIBCMT.lib relocated to the OBJ file, so that the linker uses LIBCMT.lib handle external symbols.

/ MD is "multithread- and DLL-specific version" , meaning multi-threaded DLL version, after it is defined, the compiler MSVCRT.lib relocated to the OBJ file, it is connected to the way the DLL is statically linked, in fact, work the library is mSVCR80.DLL.

which is:

Static runtime library: LIBCMT.lib

Dynamic runtime library: MSVCRT.lib + MSVCR80.DLL

Which DEBUG version as an option at the end of a lowercase "d" represents no "d" for the RELEASE version. Large-scale projects must be requested runtime library all the components and third-party libraries are unified, otherwise there will be LNK2005 blowout.

Single-threaded run-time library option / ML and / MLd after VS2003 was abolished.

 

reference:

1. Add vs in c ++ lib libraries and header files (external dependency)

2. Configure the difference between static and dynamic library, lib files and dll files

3. VS runtime MT, MD of difference

4. vc ++ runtime library selection (/ MT, / MTd, / MD, / MDd) compile time

Guess you like

Origin www.cnblogs.com/embedded-linux/p/11469900.html