Export a class. Export template STL. Vector in DLL, warning.

Source: Internet
Author: User

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

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.