当前位置:网站首页>The difference between MFC for static libraries and MFC for shared libraries

The difference between MFC for static libraries and MFC for shared libraries

2022-07-01 03:37:00 Three Belle Wenzi

    ​ Daily use MFC When developing dynamic link libraries , You may face “MFC Use ” This option , There are three options in this option , Namely :​

  • Use standards  Windows  library

  • Use... In a static library  MFC

  • Sharing  DLL  Use in  MFC

So what's the difference between the three ? Why set these three methods ?

According to the problems encountered in the development of the project , According to my own understanding, I will uncover the differences among the three .

We all know , What is a dynamic library , If you don't know, you can see what Xiaobian sorted out before Dynamic link library article , Dynamic link libraries usually do not run directly on the operating system , The suffix that he presents on our computer is  .dll, At the same time, it cannot receive messages . It is a separate file , Only in one executable (windows On is .exe) To be called to take effect . as everyone knows ,Windows API All the functions in are contained in DLL in , Among them is 3 The most important DLL Namely :

  • Kernel32.dll: Contains for managing memory 、 Functions of processes and threads , for example CreateThread function ;

  • User32.dll: Contains tasks for performing user interface ( Such as window creation and message delivery ) Function of , for example CreateWindow function ;

  • GDI32.dll: Contains functions for drawing and displaying text .

When we are creating MFC During the project , Select create dynamic link library project , After compilation, an additional import and storage file will be generated under the file path of the generation library , The suffix of the import and storage file is also “lib”, For example Serven.lib file :

however , There is an essential difference between dynamic link library and static library , To a DLL Come on , The import and storage file contains the DLL Symbolic names of exported functions and variables , and DLL The file contains the DLL Actual functions and data . In the case of using dynamic libraries , When compiling linked executables , Just link to the DLL Import library file , The DLL The function code and data in are not copied to the executable , Until the executable runs , Take the... Required for loading DLL, Will be DLL Map to the address space of the process , And then visit DLL Functions exported in . At this time , When launching products , Out of the release executable , You also need to publish the dynamic link library that the program will call , As shown in the figure below , Suppose our executable program is Servenexe.exe,DLL The library file is Serven.dll, When publishing, package the two files into a folder and publish them together .

Said so much , Let's take a look “ Use shared dynamics MFC DLL The rules of (D)” and “ With static links MFC The rules of ”.

Use shared dynamics MFC DLL The rules of (D):

When the compiler compiles , Will not put MFC Library functions to compile in , That is to say , When we're in DLL The program uses MFC Library function ( for example CreateWindows()), The compiler won't compile this function in , So this requirement is to run this dll The file must have been installed on the computer MFC Library function , That is, installation MFC Environment . If it is not installed on the computer , Then the following error message will appear :

Hint you didn't , The program did not find the corresponding MFC Library function . The disadvantage of using this method is that it requires that the computer running the library must install the corresponding MFC Library function .

Use “ With static links MFC The rules of ” The way :

When the compiler compiles , Will be able to MFC Library functions to compile in , That is to say , When we're in DLL The program uses MFC Library function ( for example CreateWindows()), The compiler will compile this function , So this does not require running this dll The file must have been installed on the computer MFC Library function , That is, installation MFC Environment . But this one has a drawback is that it is compiled dll The size of will be larger than using shared dynamics .

summary :

  • Use standards Windows library : Can only be used in non MFC In Engineering , If in MFC The project will cause code compilation errors ;

  • Sharing DLL Use in MFC: The generated program executable file is relatively small , However, it is required that the necessary... Must be installed on the target machine MFC The library files ;

  • Use... In a static library MFC: The generated program executable files are almost all Windows Can be executed , But the memory occupied by this program after it is generated will be relatively large , Because the program contains the necessary MFC The library files , It can ensure normal operation on other machines .

原网站

版权声明
本文为[Three Belle Wenzi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207010318436889.html