The secret of MFC: Where is its static library and dynamic library?

Source: Internet
Author: User
1. Dependency Walker's first secret

We have written many static and dynamic libraries in MFC. These DLL are dependent on MFC, And then we give them to others to make them dependent.

Think about the imbalance. The generated non-component dll must provide the H header, lib library, and dll library files for use by others. However, when we use the mfc dll, it seems that nothing is set, we recommend that you add MFC to our program. Although everyone knows that this world is not equal, in the computer world, freedom and equality are the first rule. It is our task to figure out How MFC is embedded into our applications and expose all dark operations.

For example, you can use the Wizard to generate an MFC Dialog Box program. Test A component and call wcslen to obtain the length of a BSTR. Is a very simple EXE; through dependency Walker, it is found that it depends on a lot of DLL. As follows:

Where are these functions? This is the secret we need to know.

  • Mfc42d. dll
  • Msvcrtd. dll
  • Mfco42d. dll
  • Oleaut32.dll
2. Composition of MFC

Uncover the mystery: MFC consists of four parts. This statement may be inaccurate. It should be said that the core underlying API of VC is composed of the following parts:

  • CRT (C Runtime): The underlying Microsoft implementation version of the C and C ++ standard libraries. The interfaces refer to the standard specifications. MFC depends on this. However, the Windows API does not depend on this and has its own internal implementation version.
  • Windows API: It depends on how Microsoft's operating system gets it. It is provided in the form of c api.
  • MFC: encapsulate windows APIs. It basically covers some CRT and all windows APIs. The Group is afx.
  • ATL: The encapsulation of Microsoft's complete entry into the Component solution. The Group is a Com group.

See the following table:

In fact, look at the VC directory structure and probably know its group.

MFC static library and dynamic library

The most popular is the use of MFC as a DLL, and also as a DLL. In this case, if you dig deep, the most likely thing to find is the mfc dll.

By referring to "naming conventions for MFC DLLs" in msdn, we can find that the dynamic link library-based MFC naming convention is: MFC [d | o | n] X [u] [D]. DLL.

The main attributes are: function module (core | Ole | dB | net), version (vc6 corresponds to 42, vs2003 corresponds to 70, vs2005 corresponds to 80, vs2008 corresponds to 90 ), whether it is Unicode or ANSI, whether it is release or debug.

Note that in the debug version, each function module corresponds to a Lib, but in the release version, it is combined into a unified lib file mfc42.lib.

In fact, there is still a static Link Library. However, its name may not be correct: [n | u] afxdw [D]. Lib

Including Unicode and non-Unicode, debug and release are differentiated.

CRT static library and dynamic library

Refer to "info: What are the C/C ++ libraries my program wocould link with?" In msdn ?".

When we call such functions as sprintf and wcslen, we rely on this library.

This can be set through compilation:

In fact, the corresponding compilation items are:/MD/ml/mt

3. Static library and dynamic library CORE

All core issues related to static and dynamic libraries and connection problems are:

  • Find out whether to reference others' libraries as static or dynamic databases;
  • Static or dynamic databases are used as libraries for others.

The so-called "take" and "people" are used by the people.

A common problem is what underlying libraries of the referenced system are, static databases or dynamic databases. Generally, the MFC program depends on the MFC library, and can selectively depend on the CRT library. Do not conflict with the static and dynamic libraries used.

Several static and dynamic libraries

In vc6, for static dynamic libraries, the components are removed as a special dynamic library, and there are still five types. Generate a demo program one by one and study the settings to see the difference.

The static library exports the H header file and the Lib file. The LIB file contains program code, which is relatively large. The header file is used during source code compilation and the Lib file is used for linking. The library file is not required during runtime.

The dynamic library exports the H header file, Lib, and DLL files. The LIB file only contains the function index location of the program, which is relatively small. The header file is used for source code compilation, the Lib file is used for linking, And the DLL file is required for running.

 

This is the MFC DLL

The DLL analysis is as follows:

In essence, we can see that there are only two types of libraries: static library and dynamic library. They indicate whether they are static databases or dynamic databases.

Because the static library is used by others, there is no import/export identifier;

The dynamic library must have an import and export identity. For DLL, there are mfc dll and common win DLL. In fact, the so-called mfc dll is only a special encapsulation of dllmain, especially to facilitate resource access. The so-called extension DLL is only intended to be compatible with C; only non-MFC functions are required to be exported.

These static libraries can be configured to use the MFC Library and the CRT library. This is used to solve what database is referenced.

For static and dynamic libraries, some compilation settings are automatically generated by the MFC wizard. But the most fundamental core is in DSP:

# Targtype "Win32 (x86) static library" 0x0104

! Message "teststaticlib-Win32 release" (based on "Win32 (x86) static library ")

! Message "teststaticlib-Win32 debug" (based on "Win32 (x86) static library ")

If you want to compile a static library into another dynamic library, or vice versa, you only need to replace the unified search in the DSP with another format. Of course, it is more encouraging to generate multiple compilation configuration items.

Methods To avoid conflicts

For static and dynamic libraries, there is always a problem in the compilation link. The root cause of these problems is that a static library and its dynamic library are introduced for a project. This will cause a conflict of the same name.

    There are two solutions:

  • Make sure that all projects are introduced in the same way to a database. This may search all projects
  • For a compiled project, if a library conflict is known, ignore the static and dynamic libraries of the library.

 

4. How to maximize the most rational use of each function library

  • If the program is an MFC program, we recommend that you use the MFC Library whenever possible; do not rely on the CRT library whenever possible.
  • If the program is only a Win32 program, we recommend that you do not use the MFC Library as much as possible, depending on the CRT library.
  • If the program is only a console program, it should be dependent on the CRT library as much as possible, and less on the win api library and the MFC library.

Principle, as few dependent libraries as possible. For functions of the same function, CRT may have, win api library, and MFC library, and select the correct library.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.