Environment:
Windows, C ++
Error description:
Socket and thread used in the project, corresponding to include header files # include <winsock2.h> and # include <windows. h>
However, many errors are encountered during compilation, almost all of which are errors such as "type/link redefinition" and "unable to find identifiers", and almost all occur in winsock2.h and ws2def. h;
For example:
Error c2011: "sockaddr_in": "struct" type redefinition
Ws2def. h
Error c2375: "accept": redefinition; different links winsock2.h
Error c3861: "socket": unable to find the identifier server. cpp
Error cause:
Header files are often included in network development.
# Include <winsock2.h>
# Include <windows. h>
If the order is incorrect (Windows. H is prior to winsock2.h), many inexplicable errors may occur.
The reason is: Windows. h contains Winsock. h header file, while a lot of content in winsock2.h will be related to Winsock. the content in H is the same, resulting in type redefinition (winsock2.h is Winsock. later versions of H)
Solution:
If the macro win32_lean_and_mean is not specified, windows. h contains Winsock. h.
Therefore, the solution is as follows:
Define the win32_lean_and_mean macro before including <windows. h>, as shown in
As shown below:
# Define win32_lean_and_mean
# Include <windows. h>
Supplement:
Windows. h contains the following content:
# Ifndef win32_lean_and_mean
# Include <cderr. h>
# Include <DDE. h>
# Include <ddeml. h>
# Include <DLGS. h>
# Ifndef _ Mac
# Include <lzexpand. h>
# Include <mmsystem. h>
# Include <nb30.h>
# Include <rpc. h>
# Endif
# Include <shellapi. h>
# Ifndef _ Mac
# Include <winperf. h>
# If (_ win32_winnt> = 0x0400)
# Include <winsock2.h>
# Include <mswsock. h>
# Else
# Include <Winsock. h>
# Endif/* _ win32_winnt> = 0x0400 */
# Endif
# Ifndef nocrypt
# Include <wincrypt. h>
# Endif
# Ifndef nogdi
# Include <commdlg. h>
# Ifndef _ Mac
# Include <winspool. h>
# Ifdef inc_ole1
# Include <Ole. h>
# Else
# Include <ole2.h>
# Endif /*! Inc_ole1 */
# Endif /*! Mac */
# Endif /*! Nogdi */
# Endif/* win32_lean_and_mean */