Complicated? This should be classified
1: Scenario 1
If the class definition contains only the built-in type variables, Int, float, and so on, or the member functions only use these variables as parameters, it is very simple.
Direct
Class _ declspec (dllexport) yourclass
{
}
That's all.
2: Case 2
If other classes are used inside the class, it is best to export other classes. Otherwise, the compilation warning will appear during the first compilation:
Warning c00001: needs to have DLL-Interface
This means that you use some other types/interfaces, but these types or interfaces are not exported. When your client uses these types/interfaces, errors will occur!
Class _ declspec (dllexport) yourclass
{
Youranatherclass m_data; // warning 4251 is displayed here. If youranatherclass is not exported.
}
Solution: add
Class _ declspec (dllexport) youranatherclass
{
}
As shown above, when your youranatherclass is not exported, the DLL user may encounter a link error.
3: Case 3
When the STL template is used inside the class, the c1_1 warning will also appear, and the situation will be different.
Class _ declspec (dllexport) yourclass
{
Vector <int> m_data; // here, the warning 4251 is displayed, because the vector <int> type is not exported.
}
The above template (whether it is an STL template or a custom template)Code, The c1_1 warning will appear during DLL compilation, but the DLL user will not see a link Error !!!
This is because the DLL user also has a set of template definitions. When they use the vector <int>, however, you also have a set of STL templates (or custom templates). You can use your own templates to instantiate what is not exported in this DLL!
Therefore, close the c00001 warning due to the use of STL (or template ).
# Pragma warning (push)
# Pragma warning (Disable: 4251)
// Your declarations that cause 4251
# Pragma warning (POP)
If you do not want to disable a warning
1) for user-defined templates
Template Class dllimportexportmacro sometemplate <int>;
Sometemplate <int> Y;
2) STL templates
Template Class dllimportexportmacro STD: Allocator <int>
Template Class dllimportexportmacro STD: vector <int,
STD: Allocator <int>;
Vector <int> m_data;
See:
Http://www.unknownroad.com/rtfm/VisualStudio/warningC4251.html