Reference: http://blog.jobbole.com/86852/
Since I'm only using Windows, the Linux section doesn't say much, so let's summarize the knowledge below windows:
Static Library
The reason for being a "static library" is because in the link phase, the resulting target file will be compiled. o Link to the executable file with the referenced library. Therefore, the corresponding link method is called static link.
Imagine that the static library is linked to an executable file along with the assembly-generated target file, then the static library must be similar to the. o file format. In fact, a static library can be simply regarded as a set of target files (. o/.obj files), that is, many of the target files are compressed and packaged to form a file. Static Library Features Summary:
- The static library link to the library is done at compile time.
- The program is no longer associated with the library at runtime and is easy to transplant.
- A waste of space and resources, as all related target files are linked to an executable file with the involved library of functions.
Create and use a static library under Windows
Create a static library (. Lib)
If you are building a static library using the VS command line, there are two steps to build the program:
- First, create a target file called "Xxx.obj" by using the Cl.exe compiled code (CL/C xxx.cpp) with the compiler option/C.
- Then, use the Library Manager Lib.exe the link code (LIB xxx.obj) to create a static library Xxx.lib
Of course, we don't usually use it, and it's easier to use the VS Engineering setup. When creating the Win32 console program, check the static library type, open the project properties panel → configuration properties → general, and configure the type to select the static library.
Build project to generate a static library.
Using a static library
Test code for Linux under the same. There are 3 ways to use:
Method One:
Use the static library method in VS:
- Project Properties panel → Common Properties → frames and references → add reference, the Add Reference dialog box appears. The Projects tab lists the individual projects in the current solution and all the libraries that can be referenced. In the Projects tab, select the library that you want to add (project is under solution?). )。 Click OK
- To add the Xxx.h header file directory, you must modify the Include directory path. Open the project properties panel → configuration properties → C + + + → general, in the additional Include Directory property value, type the path to the directory where the xxx.h header file resides or browse to the directory
If the referenced static library is not a sub-project under the same solution, but instead uses the static library Lib and header files provided by the third party, the method above cannot be set. There are also 2 method settings available.
Method Two:
Open the project properties panel → configuration properties → linker → command line, enter the full path of the static library.
Method Three:
- "Properties Panel" → "Configuration Properties" → "linker" → "general", attach dependent library directory input, static library is located in the directory;
- Properties panel → configuration properties → linker → input, additional dependent libraries Enter the static library name StaticLibrary.lib
Dynamic Library
Through the above introduction to find the static library, easy to use and understand, but also to achieve the purpose of code reuse, then why do you need a dynamic library?
Why do I need a dynamic library?
Why the need of dynamic library, in fact, is also the characteristics of static library caused.
- Space waste is a problem of static library. (Static libraries are embedded in the program, so there are multiple copies)
- Another problem is that static libraries can cause problems with updating, deploying, and publishing pages for programs. If the static library Liba.lib is updated, the application that uses it needs to be recompiled, published to the user (a small change for the player, which causes the entire program to be re-downloaded, and the whole volume updated).
A dynamic library is not connected to the target code when it is compiled, but is loaded only when the program is running. If different applications call the same library, then only one instance of the shared library is needed in memory, which avoids the problem of space wasting. Dynamic libraries are loaded only when the program is running, and also solve the problem of static libraries updating, deploying, and publishing pages. Users only need to update the dynamic library to update incrementally.
Dynamic Library Features Summary:
- The dynamic library puts the link loading of some library functions into a period of delay until the program runs.
- You can implement resource sharing between processes. (So dynamic libraries are also known as shared libraries)
- It's easy to upgrade some programs.
- It is even possible to actually do a link loading completely controlled by the programmer in the program code (display call).
window is different from the Linux executable file format, there are some differences when creating a dynamic library.
- The executable file format under Windows is the PE format, and the dynamic library requires a DllMain function to initialize the portal, usually with the _declspec (dllexport) keyword in the declaration of the exported function.
- The executable file compiled by the Linux GCC is the elf format, which does not need to initialize the entrance, and does not need the function to make special declaration, it is convenient to write.
Unlike creating a static library, you do not need a packaging tool (AR, lib.exe) to create a dynamic library directly using the compiler.
Creating and using dynamic libraries under Windows
Create a dynamic library (. dll)
First, a DllMain function is required to initialize the portal (when the Win32 console program is created, the file is automatically generated by checking the DLL type):
1 //Dllmain.cpp:Defines the entry point for the DLL application.2#include"stdafx.h"3 4 BOOL apientry DllMain (hmodule hmodule,5 DWORD Ul_reason_for_call,6 lpvoid lpreserved7 )8 {9 Switch(Ul_reason_for_call)Ten { One CaseDll_process_attach: A CaseDll_thread_attach: - CaseDll_thread_detach: - CaseDll_process_detach: the Break; - } - returnTRUE; -}
The _declspec (dllexport) keyword is typically required when exporting a function's declaration:
#ifdef Xxx_exports
#define XXX_API __declspec (dllexport)
#else
#define XXX_API __declspec (dllimport)
#endif
Building a dynamic library requires setting the project properties, opening the project properties panel → configuration properties → general, and configuring the type to select the dynamic library.
Build project to generate a dynamic library.
Working with Dynamic libraries
Method One:
- Project Properties panel → Common Properties → frames and references → add reference, the Add Reference dialog box appears. The Projects tab lists the individual projects in the current solution and all the libraries that can be referenced. In the Projects tab, select the dynamic library (the project is also in the project?). )。 Click OK
- To add the XXX.h header file directory, you must modify the Include directory path. Open the project properties panel → configuration properties → C + + + → general, in the additional Include Directory property value, type the path to the directory where the XXX.h header file resides or browse to the directory
Method Two:
- "Properties Panel" → "Configuration Properties" → "linker" → "general", additional dependent library directory input, the directory where the dynamic library
- "Properties Panel" → "Configuration Properties" → "linker" → "input", additional dependent library input dynamic library compiled XXX.lib
There may be a question here, how does a dynamic library have a XXX.lib file? That is, whether it is a static link library or a dynamic link library, and finally there are LIB files, then what is the difference between the two? In fact, the two are completely different things.
The lib file corresponding to the static library is called the Static library, and the lib file corresponding to the dynamic library is called "Import library". In fact, the static library itself contains the actual execution code, symbol table and so on, and for the import library, its actual execution code is located in the dynamic library, the import library contains only the address symbol table, etc., to ensure that the program to find some basic address information of the corresponding function.
explicit invocation of a dynamic library
The dynamic library usage method described above is implicitly called by the Static Library, which specifies the appropriate library and lookup path when compiling. In fact, dynamic libraries can also be called explicitly. "In C language", show call a dynamic library easily!
To explicitly invoke a dynamic library under Windows
The application must make a function call to explicitly load the DLL at run time. To explicitly link to a DLL, an application must:
- Call LoadLibrary (or similar function) to load the DLL and get the module handle.
- Call GetProcAddress to get a pointer to the function for each exported function that the application is calling. Because the application is a function of invoking a DLL through pointers, the compiler does not generate external references, so you do not need to link to the import library.
- Call FreeLibrary when you are finished using the DLL.
Explicitly calling C + + Dynamic Library Note points
For C + +, the situation is slightly more complicated. Part of the difficulty of explicitly loading a C + + dynamic library is because C + + 's name mangling, and partly because it does not provide a suitable API to load classes, in C + + you might want to use a class in the library, which requires creating an instance of the class, which is not easy to do.
The name mangling can be resolved by extern "C". C + + has a specific keyword used to declare a function with C binding: extern "C". Functions declared with extern "C" will use a function name, just like a C function. Therefore, only non-member functions can be declared as extern "C" and cannot be overloaded. Despite the limitations, extern "C" functions are useful because they can be dynamically loaded like C functions by Dlopen. With the extern "C" qualifier, it does not mean that C + + code is not available in the function, but instead it is still a complete C + + function that can use any C + + attribute and various types of parameters.
Summary
The difference between the two is that the code is loaded in a different time.
- The static library is connected to the target code when the program is compiled, and the static library is no longer needed when the program is run, so the volume is large.
- Dynamic libraries are not connected to the target code when the program is compiled, but are loaded only when the program is running, so a dynamic library exists when the program is run, so the code is small.
The advantage of a dynamic library is that if different applications call the same library, then only one instance of the shared library is needed in memory. Bring the benefits, there will be problems! such as the classic DLL hell problem, about how to avoid the dynamic library management problems, you can find the relevant information on their own.
"Go" about dynamic libraries and static libraries