Export a C ++ class from a common DLL (2)-a closer look at the underlying mechanism of the C ++ class

Source: Internet
Author: User
Balon vernacular MSDN: export C ++ classes from common DLL (2)-a closer look at the underlying mechanism of exporting C ++ classes in the previous article, we introduced how to export C ++ classes from a DLL and how to selectively export C ++ class members. So what is the underlying mechanism of the entire system? In what ways can we use the class exported from one DLL in another program? We know that to use a C ++ class, the necessary condition is that the header file of this class can be obtained during the compilation period, you can also find the link addresses of the corresponding symbols (such as member functions and static data members) during the link period ). If the C ++ class is in the same project as your user, this condition is well met: first, the header file of the C ++ class is well obtained. Directly include the Class header file from the user. The C ++ class is often used by the compiler as a compilation unit to generate an obj file. During the final link process, the linker will link all obj links in the project to generate the final binary target file. Therefore, when the linker encounters a class member function (or other forms of symbolic reference), it will find the symbolic link address in the obj file generated by this class. So what is the need to use a C ++ class in the Code during the compilation and link phases? In other words, the compiler and the linker will not complain about what conditions are met? According to the definition of C ++, a C ++ class declares or defines the following types of content: 1.
Declares a data structure, non-static data members in the class, virtual table entry address pointers that cannot be seen in the Code but will be generated if there is a virtual function. 2.
Declare and define a bunch of functions. Their first parameter is a pointer to the data structure. These are actually non-static member functions (including virtual functions) in the class. Although they are written inside a pair of braces in the class declaration, however, nothing is actually added to the internal data structure described in the preceding 1st. In fact, such a declaration only adds two attributes to these functions: the scope of the function name identifier is limited to the class; the first parameter of the function is this, which is ignored. 3.
Declare and define another bunch of functions. They seem to be some common functions and have almost no relationship with this class. These are actually static functions in the class. They are the same. They do not add anything to the internal data structure described in Article 1st, but the scope of the function name identifier is limited to the class. 4.
Declares and defines a bunch of global variables. These are actually static data members in the class. 5.
Declares and defines a global variable, which is an array of function pointers to save the entry addresses of all such virtual functions. Of course, the premise for generating this global variable is that this class has virtual functions. The following is an example. Class MyClass
{
Public:
Int x;
Int y;
Void Foo ();
Void Bar (int newX, int newY );
Virtual void VFoo ();
Virtual void VBar (int newX, int newY) = 0;
Static void SFoo ();
Static void SBar (int newX, int newY );
Static int sx;
Static int sy;
}; For this class MyClass listed above, most C ++ compilers compile in the following way: now let's take a look at why the compiler needs header files and symbolic addresses to compile a program that uses MyClass. First, the compiler needs to know the memory layout of the class during the compilation period to ensure that the correct code for opening up the memory and the sizeof (MyClass) values can be generated. With the header file, the compiler will know that a MyClass occupies 12 bytes of memory (see two integers and a pointer ). Secondly, when calling member and static functions of MyClass, the linker needs to know the entry address of these functions. If the entry address cannot be provided, the linker reports an error. Finally, when referencing static data members of MyClass, the linker needs to know the addresses of these variables just like referencing an external global object. If the address of these variables cannot be provided, the linker also reports an error. We can see that: 1.
Compilation period: the Class header file must be provided so that the compiler can know the size and memory layout of the class instance. 2.
Link period: You must provide the addresses of member functions, static functions, and static data members referenced in the program so that the linker can correctly generate the final program. Here, we can guess that, in fact, to export a class, the compiler actually only needs: member functions, static functions, and static data members can be exported as common functions and global variables. In other words, we do not actually "export a class", but export the endpoint address of the "defined entity" to be referenced in this class as normal functions and variables. Finally, let's take a look at the generated DLL for exporting the MyClass class in the previous column. You can see the following result by using Dependence: except that the VBar function is a pure virtual function, the entry addresses of other functions and static data members are exported. In addition, we can see that vtable is also exported for reference when operating virtual functions.

Balon vernacular MSDN: export C ++ class (1)-How to Use dllexport and dllimport from common DLL (Chinese-English comparison and note solution) I am a little lazy when writing this content, this avoids a lot of complex contents in virtual tables. Thanks to houdy's prompt, he analyzed the underlying mechanism of virtual tables and exported virtual tables from DLL in detail in his article. To answer this question, you must take a look: where is the virtual function table? From: http://blog.csdn.net/BalonFan/article/details/1603836
Related Article

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.