Export of static member variables

Source: Internet
Author: User

When writing a DLL, use _ declspec (dllexport) to reduce the need to manually define which functions to export in the def file. Of course, if your dll contains all c ++ classes, you cannot specify the export function in def. Instead, you can use _ declspec (dllexport) to export the class. However, the description of _ declspec (dllimport) in the msdn document is a bit strange. Let's take a look at what is said in msdn: "Do not use _ declspec (dllimport) the code can also be compiled correctly, but the use of _ declspec (dllimport) enables the compiler to generate better code. The compiler can generate better code because it can determine whether the function exists in the DLL, which allows the compiler to generate code that skips the indirect addressing level, these codes usually appear in function calls across DLL boundaries. However, you must use _ declspec (dllimport) to import the variables used in the DLL ". First, it seems that this section above means that you can use DLL export without using it, but the last sentence also says that _ declspec (dllimport) must be used) to import the variables used in the DLL. What does this mean ?? Let me try it out. Assume that you only export a simple class in DLL. Note that I assume that you have defined simpledll_export export ------------------ IN THE PROJECT properties ---------------------------------------------------------------------------------------------------------------------------------------------------
Simpledllclass. h # ifdef simpledll_export
# Define dll_export _ declspec (dllexport)
# Else
# Define dll_export
# Endifclass dll_export simpledllclass
{
Public:
Simpledllclass ();
Virtual ~ Simpledllclass (); Virtual getvalue () {return m_nvalue ;};
PRIVATE:
Int m_nvalue;
};
Simpledllclass. cpp # include "simpledllclass. H" simpledllclass: simpledllclass (){
M_nvalue = 0;
} Simpledllclass ::~ Simpledllclass (){
} Else }---------------------------------------------------------------------------------------------------------------------------------------------------
Then you can use this DLL class to include simpledllclass in your app. h, your app project does not need to define simpledll_export. Therefore, dll_export will not exist. At this time, you will not encounter problems in the app. This corresponds to the definitions of _ declspec (dllimport) in msdn. However, we have not encountered that variables cannot be used normally. Well, let's change simpledllclass, change its m_nvalue to static, and then add an int simpledllclass: m_nvalue = 0 in the CPP file;
If you don't know why you want to add this line, go back and look at the basics of C ++. After the change, link your app and check the result. The result is that link tells you that the m_nvalue cannot be found. Why is there no such thing as clearly defined ?? It must be because I defined m_nvalue as static. But if I must use the design pattern of Singleton, then this class must have a static member, and there is no link each time. Isn't that all done? If you have a platform
SDK. Use the depend program in it to check whether the m_nvalue is exported in the DLL.
Go back and check out the last sentence I mentioned in msdn. Let's change simpledllclass. H to the following: # ifdef simpledll_export
# Define dll_export _ declspec (dllexport)
# Else
# Define dll_export _ declspec (dllimport)
# Endif
Link again. Everything works normally. Originally, dllimport was used to better process static member variables in the class. If there is no static member variable, this _ declspec (dllimport) does not matter. ========================================================== ==================================== _ Declspec (dllexport) and _ declspec (dllimport)All are DLL keywords, that is, export and import. They are used to export and import DLL classes and functions as well as data. The main difference is that dllexport is used in the declaration of these classes, functions and data to indicate that these things can be used by external functions, namely (dllexport) is to expose the relevant code (class, function, data) in the DLL for other applications. When the (dllexport) keyword is used, it is equivalent to declaring the relevant content following the (dllexport) keyword for other programs. The dllimport keyword is used when an external program needs to use DLL-related content. When an external program uses the DLL internal code (class, function, global variable), it only needs to use the (dllimport) keyword inside the program to declare the required code, that is, the (dllimport) keyword is used only when external programs need to use the DLL internal content. ( Dllimport) is used to insert the relevant code in the DLL into the application.. _ Declspec (dllexport) and _ declspec (dllimport) echo each other. You can use dllimport to import relevant code in external functions only after the dllexport is declared in the DLL. In fact, when an application accesses the DLL, it is actually a link between the import function in the application and the export function in the DLL file. There are two link Methods: implicit link and explicit link. Implicit Link: It refers to the link address provided by the compiler to the application about the DLL name and the DLL function, and does not need to explicitly load the DLL to the memory in the application, that is, the use of dllimport in the application indicates the use of implicit links. However, not all implicit links use dllimport. Explicit link:When the application loads the DLL explicitly using a statement, the compiler does not need to know any information about the DLL. /*************************************** **************************************** ******************************//******** **************************************** **************************************** *********************//***************** **************************************** **************************************** ************/

DLL export functions in two ways dll can use two methods to import public symbols into the application or export functions from dll: Use the module definition when generating DLL (. def) file. Use the _ declspec (dllimport) or _ declspec (dllexport) keywords in the function definition of the main application. Use the. Def file: the module definition (. Def) file is a text file that contains one or more module statements describing various DLL attributes. If you do not use _ declspec (dllimport) or _ declspec (dllexport) to export the DLL function, the DLL requires the. Def file. Use _ declspec: 32-bit visual c ++ use _ declspec (dllimport) and _ declspec (dllexport) replace the _ export keyword used in the previous 16-bit visual c ++. _ Declspec (dllexport) Anyone who writes a Win32 program or has done a DLL knows the role of _ declspec (dllexport, it is a method to manually define which functions to export in the def file. Of course, if your dll contains all c ++ classes, you cannot specify the export function in def. Instead, you can use _ declspec (dllexport) to export the class. Description of _ declspec (dllimport) in the msdn document:

You can compile the code correctly without using _ declspec (dllimport), but using _ declspec (dllimport) enables the compiler to generate better code. The compiler can generate better code because it can determine whether the function exists in the DLL, which allows the compiler to generate code that skips the indirect addressing level, these codes usually appear in function calls across DLL boundaries. However, you must use _ declspec (dllimport) to import the variables used in the DLL. First, it seems that this section above means that you can use DLL export without using it, but the last sentence also says that _ declspec (dllimport) must be used) to import the variables used in the DLL. During windows DLL programming, you can use the _ declspec (dllimport) keyword to import functions or variables. Function Import when you need to use functions in the DLL, the compiler can automatically import functions without displaying them. However, if you import functions explicitly, the compiler will produce code of better quality. Since the compiler knows exactly whether a function is in a DLL, it can produce better code without indirect call transfer. In Win32 PE format (portable executable format), all import addresses are placed in one import address table. The following example shows the difference between using _ declspec (dllimport) to import a function and not using it: Assume that func is a feature in a dll. currently, the main function of .exe is used to call func, if you do not explicitly import the func function (that is, there is no :__ declspec (dllimport), the code example is as follows: int main () {func ();} the compiler will generate a call code like this: Call func, then the linker translates the call into code like this: Call 0x40000001; ox40000001 is the address of "func" and, the linker will generate a thunk, such as: 0x40000001: jmp dword ptr under imp_func. imp_funcis the address of the function slot in the import address table of func.pdf in .exe. Then, the local machine only needs to update the import address table of .exe when the local machine is running. If _ declspec (dllimport) is used to import functions explicitly, the linker will not generate thunk (if not required) and directly generate an indirect call. Therefore, the following code: _ declspec (dllimport) void func1 (void );

Void main (void)

{

Func1 ();

} The following call command will be called: Call dword ptr _ imp_func1. Therefore, the explicit import function can effectively reduce the target code (because Thunk is not generated ). In addition, functions other than dll can also be used in DLL to improve space and time efficiency. The import of variables is different from that of functions. When using the variables in DLL, You need to import the variables explicitly. Use the _ declspec (dllimport) keyword to import a variable. If you use. Def in the DLL to export the variable, you should use data to modify the variable instead of using the abandoned constant. Because constant may need to use pointers to indirectly access variables, it is not sure when the problem will occur. Source: http://chenuaizhang2008.blog.163.com/blog/static/7276357201011625214953/

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.