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.