C ++: the difference between the use of DLL and LIB

http://www.cppblog.com/biao/archive/2013/03/14/198416.html

There are two libraries:
one is the LIB contains information about DLL files and functions are located in the function of the position (entry), code is loaded by the runtime DLL in the process space provided, known as dynamic link library dynamic link library.
One is LIB contains the function code itself, directly to the code to program them at compile time, called the static link library static link library.
There are two ways link:
Dynamic links dynamic link library that allows executable module (.dll files or .exe file) contains only the information required to locate DLL function at run-time executable code.
Statically linked use of static link library, LIB linker get all referenced functions from the static link library, put the executable file and the library together with the code.

About the difference between lib and dll as follows:
(1) is used when compiling lib, dll is used at runtime. To complete source code to compile, just lib; if you want dynamically linked programs up and running, only dll.
(2) If there dll file, lib are generally some of the index information, record the entry and location of the dll function, the dll is the specific content of the function; if only lib file, then the lib file is a static compiler of the index and implementations in them. Use statically compiled lib file, do not need to run the program and then hang a dynamic library, the disadvantage is leading to large applications, but also lose the flexibility of dynamic libraries, to publish a new application for the job when the new version is released.
Case (3) dynamic link, there are two files: a LIB file is a DLL file. LIB contains the function name and location of the DLL to be exported, DLL contains the actual functions and data, applications use LIB file is linked to the DLL file. In the application's executable file, stored in function code it is not being called, but the address of the corresponding DLL function code, thus saving memory resources. LIB and DLL files must be issued together with the application, otherwise the application will generate an error. If you do not want to use the lib lib file or not file, you can use WIN32 API functions LoadLibrary, GetProcAddress load.

Lib files should be noted that the use of two:
(. 1) .h header file containing the class data structures or symbol prototype or lib described output. Application calls lib, need the file that contains the application's source files.
(2) .LIB file slightly.

Dll file using three should be noted:
(. 1) .h file header, or .h file contains the class prototype symbols or data structures and the description of the output dll. Application calls dll, need the file that contains the application's source files.
(2) .LIB files are dll after compiling, linking success generated files action when other application calls dll, need the file into your application, otherwise an error. If you do not want to use the lib lib file or not file, you can use WIN32 API functions LoadLibrary, GetProcAddress load.
(3) .dll files, real executable file, the application developed at the time of publication, only to have the .exe file and .dll files, the file does not need .lib and .h header files.

Use lib method:
static lib in a lib file is actually a collection of any number of files obj, obj file is generated cpp file to compile. When compiling such a static library project, the link will not encounter an error; even if wrong, it will only exposed during use of this lib file or DLL project in EXT.
In the new VC type in a static library project Lib, file and adding test.cpp test.h files (including function declarations in the header file), then compiled, generated Lib.lib file.
To use this lib other works in two ways:
(1) adding Lib.lib file project-> link-> Object / Library Module in (first check the project directory, then Lib directory inquiry system); or in the source code Add instruction #pragma comment (lib, "Lib.lib" ).
(2) the directory Lib.lib copy into the project is located, or execute files generated directory or the system Lib directory.
(3) adding the corresponding header file test.h.

Use DLL methods:
the use of dynamic link lib, obj is not a collection of files, that there will not be the actual implementation, it only provides a dynamic link to the DLL information needed, such lib can compile a DLL project by the the compiler generates.
Methods DLL project (slightly) created.
(1) implicit link
A first method is: by project-> link-> Add .lib files Object / Library Module (or added in the source code instruction #pragma comment (lib, "Lib.lib" )), and .dll files into the directory where the project, and then add the corresponding .h header files.

#include "stdafx.h"
#include "DLLSample.h"

#pragma the Comment (lib, "DLLSample.lib") // You can also set up libraries in the project properties link

int main ()
{
        TestDll (123); / / dll functions that are declared in the DllSample.h
        return (. 1);
}

(2) explicit links
need WIN32 API function pointers and functions LoadLibrary, GetProcAddress loading, using this method of loading, and does not require .lib .h file header files, can only .dll file (.dll file into the the project directory).

#include <iostream>
#include <windows.h>         //使用函数和某些特殊变量
typedef void (*DLLFunc)(int);
int main()
{
        DLLFunc dllFunc;
        HINSTANCE hInstLibrary = LoadLibrary("DLLSample.dll");

        if (hInstLibrary == NULL)
        {
          FreeLibrary(hInstLibrary);
        }
        dllFunc = (DLLFunc)GetProcAddress(hInstLibrary, "TestDLL");
        if (dllFunc == NULL)
        {
          FreeLibrary(hInstLibrary);
        }
        dllFunc(123);
        std::cin.get();
        FreeLibrary(hInstLibrary);
        return(1);
}

LoadLibrary function use a name as a parameter, obtain an instance of the DLL (HINSTANCE type handle instance), usually called after the function needs to look at the function returns successful, if unsuccessful returns NULL (handle is invalid), then call the function FreeLibrary DLL release memory available.
Using GetProcAddress function DLL function name and the handle as a parameter and returns the corresponding function pointer, and must use strong rotation; determining whether the function pointer is NULL, if a function is called to release the memory DLL FreeLibrary obtained. After that, you can use the function pointer to call the actual function.
Finally, remember to use the FreeLibrary function to release memory.

Note: How to find the DLL file an application?
Use LoadLibrary explicit link, you can specify the full path to the DLL file in the function parameters; if you do not specify a path, or implicitly link, Windows will follow the following search order to locate the DLL:
(1) the directory containing the EXE file
(2) the project directory
(3) Windows system directory
(4) Windows directory
(5) listed in the Path environment variable in a series of directories

the From:  http://www.cppblog.com/ming81/archive/2013/03/04 /198215.html

.h header files is a must compile-time, lib is needed link, dll is needed at runtime.

Additional Dependencies .lib is not a .dll, if the generated DLL, then certainly generated LIB file. If you want to compile and link the source code, header files and lib enough. If the program makes a dynamic connection up and running, there are dll enough. In the development and debugging phase, of course, the best there.

.H .lib .dll relationship between the three is:

H file role is to: Declare Function Interface 

DLL files role is: function executable code 

When we refer to a H file functions in your own programs, chaining is how to know which DLL files that call it this is the role LIB file:? Function tells the linker call in which the DLL function to execute code What position in the DLL, which is why the need for additional dependencies .LIB files, it acts as a bridge. If you generate a static library files, there is no DLL, only lib, then also in part a function of the executable code lib file

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, or you may need to call in their own LoadLibary DLL file, and then manually GetProcAddress to obtain the corresponding function of. 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.

General dynamic library program have lib files and dll files. lib file is a must even received an application at compile time, but dll file is run will be called. If there dll file, then the corresponding lib file is usually some of the index information, the specific implementation in the dll file. If only the lib file, then this is statically compiled lib file out, and implementation in which the index. Statically compiled lib file is good: there is no need for the user to install a dynamic library and then hang up. But there are drawbacks that led to large applications, but also lose the flexibility of dynamic libraries, during version upgrades, and to publish a new application for the job. In the case of dynamic library, there are two files, a library is introduced (the .LIB) file is a DLL file, import library file contains the names and location of the function exported by the DLL, and the DLL function contains the actual data, applications using the LIB file is linked to the need to use the DLL files, library functions and data is not copied into the executable file, so the application's executable file, the function code is not being called stored, but the memory address of the function in the DLL to be called, so that when one or more applications are then run program code and the function code is called linked, thus saving memory resources. As can be seen from the above description, DLL, and .LIB files must be issued together with the application, otherwise the application will generate an error.

Static library and shared library is a collection of obj file, but statically linked, there is the implementation of the program they need a copy of obj, and the dynamic linker, the implementation of the program just contains a reference to the shared library. Shared library is equivalent to a combination of a plurality of obj file obj files from all of its code is loaded after the link, whether wanted or unwanted.
May draw a conclusion:
  the program after statically linked with storing large space than the dynamically linked, since the implementation of the program contains a library copy the code;
  and dynamically linked programs large used to run space than statically linked, because it the code does not need to be loaded into the running space.


For two knowledge questions above:

1) and the .LIB DLL files must be issued together with the application, otherwise the application will generate an error. 
My answer: lib should not need it.

2) If a program is called a dynamic link library (invoked through header files, lib + dll), the function of the contents of a dynamic library changes, but the interface does not change, this is called dynamic library program recompile connections? If you are dynamically loaded by loadlibary, you need to recompile connections?

My answer: The header + lib + dll call, then recompile connection, but is used by loadlibrary, it does not need to be recompiled connection.

The second wrong answer should be no need to recompile the same interface, then .lib do not update
the From:  http://www.cppblog.com/mzty/archive/2010/11/28/134901.html

    1) to save memory . With a software module, if in the form of source code reuse, it will be compiled into different executable programs, while those running these exe binary code modules are loaded into memory repeated. If you use a dll, only loaded once in memory, all processes using this dll will share this piece of memory (of course, like dll global variables this kind of thing would be a copy of each process).
    
    2) do not need to compile software system upgrade, if the system uses a software dll, the dll is changed (function name change), system upgrades only need to replace this dll, do not need to recompile the entire system. In fact, many software is upgraded in this way. For example, we used to play Starcraft, Warcraft and other games the same way for version upgrades.
    
    3) Dll library can be used for a variety of programming languages, for example, with the c dll can call in vb. This is also not doing enough on the DLL, we invented a technology based on COM dll on better solve a series of problems.

Guess you like

Origin www.cnblogs.com/zkwarrior/p/10948174.html