Original address: https://my.oschina.net/SysuHuyh5LoveHqq/blog/644622
Recently developed a program under Windows, you need to generate static files (Lib) and dynamic library files (DLLs), which refer to a lot of posts, and some are not very clear understanding, first will I practice the record, for the latter's own query, but also for you to comment.
I. Generation and use of LIB files
1. Lib Generation
Relatively speaking, the static library files are relatively easy to generate and use, in the code, it seems that there is no need to change what, for example:
The header file function declares the following form:
extern bool Uninitialize();
In the VS2010 project, "Create a static library with Project->win32 project", configure the corresponding project properties.
Roughly including (according to individual project needs to configure, how to configure can refer to other online posts, more):
"Configuration Properties, general--character set--Choose the character set you want"
"VC + + Directory includes directory"
"VC + + directory, library directory"
After the project is generated, the corresponding LIB file is generated in the project directory, which is the real static library file. (the Lib file generated in the DLL is still different, which requires special attention when the DLL is generated)
2, LIB file use, without too much configuration, only in the "VC + + Directory directory" to introduce the required LIB files can be
Second, the DLL file generation and use
1, DLL generation
Dynamic library file DLL generation is not so simple, a little need to modify a bit of the source code (compared to, or more like the Linux so file generation method, completely do not need to change the source code, compile command modification can)
Similarly for example, the Windows source code example, also for the uniitialize function, we need to add DLL export macro is set at the function declaration:
#Ifdef__cplusplus#DefineEXPORT externC__declspec (dllexport)#Else#DefineEXPORT __declspec (dllexport)#endif. h file Function declarationEXPORTextern bool uninitialize (); . cpp file functions implemented export < Span class= "hljs-function" >bool uninitialize () {} /span>
Also in the VS2010 project properties, "New Project->win32 Project->dll", we also need to set the project properties, which are basically similar to the static library, and the dependent LIB files can be set in linker-input.
After the project is built, the corresponding DLL file and a Lib file are generated in the project directory, note that the Lib file here is not a static library file, but rather a corresponding function of the DLL file to import the library file.
2, the use of DLLs
DLL is used in two ways, one is implicit, the other is display use, here I only talk about the implicit use of my own experiment, Because this way does not need my demo program to change the interface when the interface is used (that is, do not need LoadLibrary and GetProcAddress, etc.), on the display of the method of invoking DLL interface, you can refer to many other posts on the internet description
. h file, function declaration or export required#Ifdef__cplusplus#DefineEXPORT externC__declspec (dllexport)#Else# Define export __declspec (dllexport) # Endifexport extern bool uninitialize
When using the DLL implicitly, we still need to export the macro definition for the header file of the interface.
Note: When you configure the project properties, the corresponding LIB file generated by the DLL (that is, the DLL's function import library file) is required by the "VC + + directory" to be introduced, while the DLL files in the project directory exe corresponding folder can be
An additional explanation:
https://segmentfault.com/q/1010000000431206
Q: I now have a DLL project, but just build the DLL file, I also want to generate a Lib file, what should I do? is in this DLL project, at the same time to get the DLL and LIB, do not need to create another static link library project.
A: You need at least one in your project to export symbol
linker
create a file for you .lib
.
You can precede the symbols you want to export __declspec(dllexport)
, telling linker
you that you need to export a symbol (functions, classes, and so on). Then rebuild the project and the .lib
file should be created.
For example, this:
__declspec(dllexport) void hello(void) {...}
[Turn]windows VS2010 lib and DLL file generation and use