| Back to logs list
152766 2010 年 11 月 12 日 10:03 Reading (loading. ..) Comments (0) Category: MSN-computer
from:
I believe that people who write WIN32 program, did DLL, will be very clear __declspec (dllexport) role, it is saved in the DEF file to manually define which functions to export of a method. Of course, if all your DLL in C + + class, you can not be exported are specified in the DEF function, can only use __declspec (dllexport) to export class. However, MSDN documentation which, for __declspec (dllimport) description of people feel a bit strange, first to see how the MSDN which says:
not use __declspec (dllimport) can compile the code correctly ,
air force one high, but using __declspec (dllimport) so that the compiler can generate better code. The reason why the compiler can generate better code because it can determine whether the function exists in the DLL, which makes the compiler can generate code to skip levels of indirection, and the code usually appears in the cross-border functions DLL call. However, you must use __declspec (dllimport) to import the DLL using the variables.
first glance, this passage in front of the mean, it can not properly use the DLL export library, but the last word he said,
nike air force 1, must use __declspec (dllimport) to import the DLL used variables that mean? ?
then I come to test, assuming that you just exported in the DLL in a simple class, pay attention, I assume you have defined in the project properties SIMPLEDLL_EXPORTSimpleDLLClass.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 in your include SimpleDLLClass.h APP,
air force one low nike, the APP's projects do you define SIMPLEDLL_EXPORT Therefore, DLL_EXPORT would not exist,
air force 1 shoes, and this time, you APP,
air force one low, does not have a problem. This corresponds to MSDN just said the __declspec (dllimport) can be defined normal or not. But we have not encountered the variable is not working ah. Well, we change it SimpleDLLClass, it's m_nValue into static, then in the cpp file, add a line
int SimpleDLLClass:: m_nValue = 0;
If you do not know why this line to add , then go back and look at C + + foundation. Change after this LINK to go about your APP, to see the outcome, the result is the LINK to tell you can not find this m_nValue. Clearly been defined, why has not it? ? Must be because I m_nValue defined as static reasons. But if I have to use the Design Pattern Singleton, then surely this is to have a static class member, each time LINK is not, it is not finished? If you have Platform SDK,
nike air force one, with a look inside the Depend program, DLL, he also does have the m_nValue exported ah. MSDN then go back and see me quote the last sentence of that section then. That we changed what SimpleDLLClass.h, the period of change like the following:
# ifdef SIMPLEDLL_EXPORT # define DLL_EXPORT __declspec (dllexport) # else # define DLL_EXPORT __declspec (dllimport) # endif and then LINK, all normal. The original in order to better deal with dllimport is a static class member variable, if not a static member variable, then the __declspec (dllimport) does not matter.