Type redefinition error occurred after containing winsock2.h

Source: Internet
Author: User

Problem description]
In projects that include <windows.h> and <winsock2.h>, compilation sometimes appears as
Next error:
Error C2011: ' fd_set ': ' struct ' type redefinition
Error C2011: ' timeval ': ' struct ' type redefinition
....
Error C2375: ' Accept ': redefinition; Different linkage
[Cause analysis]
The main reason is that the <winsock.h> header file is included in <windows.h> because its version
This difference leads to the above error. The relevant code in <windows.h> is as follows:
#ifndef Win32_lean_and_mean
#include <cderr.h>
#include <dde.h>
#include <ddeml.h>
........
#ifndef _mac
#include <winperf.h>
#include <winsock.h>
#endif
.......
#include <commdlg.h>
#endif
#endif
[Solution]
The above code can be seen if the Win32_lean_and_mean macro is not defined before the
The windows.h may contain a winsock.h header file, so we come to a very simple
The solution is to define Win32_lean_and_mean macros before including <windows.h>, such as
Shown below:
#define Win32_lean_and_mean
#include <windows.h>

k1988 Note: Another solution is to refer to the winsock2.h containing statements to the front containing the windows.h.

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.