In the source code of the cocos2d-x, you can often see the use of macro CC_DLL, for example in the definition of the class CCScene: classCC_DLLCCScene: publicCCNode {public: CCScene (); virtual ~ CCScene (); boolinit (); staticCCScene * create (void);}; In the cocos2d-x, macro CC_DLL is defined differently based on different platforms,
In the source code of the cocos2d-x, you can often see the use of macro CC_DLL, for example in the definition of the class CCScene: classCC_DLLCCScene: publicCCNode {public: CCScene (); virtual ~ CCScene (); boolinit (); staticCCScene * create (void);}; In the cocos2d-x, macro CC_DLL is defined differently based on different platforms,
In the source code of the cocos2d-x, we can often see the use of macro CC_DLL, such as in the definition of Class CCScene:
class CC_DLL CCScene : public CCNode{public: CCScene(); virtual ~CCScene(); bool init(); static CCScene *create(void);};
In the cocos2d-x, according to different platforms, macro CC_DLL definition is different, in iOS/Android/Blackberry/Mac platform, CC_DLL represents "null ":
#define CC_DLL
On the win32 platform, CC_DLL is defined:
#if defined(_USRDLL) #define CC_DLL __declspec(dllexport)#else /* use a DLL library */ #define CC_DLL __declspec(dllimport)#endif
On linux, CC_DLL is defined:
#if defined(_USRDLL)#define CC_DLL __attribute__ ((visibility ("default")))#else /* use a DLL library */#define CC_DLL __attribute__ ((visibility ("default")))#endif
For win32, you need to understand the functions of _ declspec (dllexport) and _ declspec (dllimport.
_ Declspec (dllexport)
Declare an export function to export the function from the local DLL. I want to use it for others. This method is generally used in dll 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.
// 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(){}
For the above Code, if SIMPLEDLL_EXPORT is defined, the above Code will be compiled to generate a dll file, which will provide functions and variable calls for SimpleDLLClass to other program modules.
_ Declspec (dllimport)
Declare an import function, that is, this function is imported from another DLL. I want to use it. It is generally used in the exe of a dll.
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.
During Windows DLL programming, you can use the _ declspec (dllimport) keyword to import functions or variables.
Function Import
When you need to use a function in a DLL, the compiler can automatically import the function without displaying it. 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 differences between using _ declspec (dllimport) to import functions and not using them:
Assume that funcis the identifier in a dll. currently, the main function of the .exe to be generated calls the func function and imports the func function without display (I .e., no :__ declspec (dllimport). The sample code 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 similar to this:
Call 0x40000001; // 0x40000001 is the func address.
In addition, the linker will generate a Thunk, such:
0x40000001: jmp dword ptr _ imp_func
In this example, 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 display 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.
Variable Import
Different from functions, when using a variable in a DLL, you must import the variable 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.
I believe that anyone who writes a WIN32 program, once working on a DLL, knows the role of _ 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:
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. What does this mean ??
Let me try it out. Assume that you only export a simple class in the DLL. We will import and use the class in this DLL in the following code. Note, I suppose you have defined SIMPLEDLL_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(){}
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 m_nValue to static, and add an int SimpleDLLClass: m_nValue = 0 in the cpp file. If you don't know why to add this line, then go back to 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 the Platform SDK, use the Depend program 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.