Error reporting reason:
We know that Windows network programming requires at least two header files: winsock2.h and windows.h, and there is an old version of winsock.h before WinSock2.0. It is the order in which these three header files are included that causes the above problems.
The content of winsock2.h has the following macro definitions at the beginning of the file:
#ifndef _WINSOCK2API_ #define _WINSOCK2API_ #define _WINSOCKAPI_ /* Prevent inclusion of winsock.h in windows.h */ ...... ... ...... /* * Pull in WINDOWS.H if necessary */ #ifndef _INC_WINDOWS #include <windows.h> #endif /* _INC_WINDOWS */
This is the most common protection measure to prevent repeated inclusion of header files.
The definition of _WINSOCKAPI_ prevents the inclusion of the old file winsock.h, that
is, if the user includes winsock2.h first, then it is not allowed to include winsock.h. The
second half of the effect is: if the user does not include windows.h ( _INC_WINDOWS (defined in windows.h) automatically includes it to define the types and constants required by WinSock2.0.
Now switch to windows.h, we will find the following: (Note: here is the difference between windows.h in MS’s Platform SDK and the file that comes with VC)
#ifndef _MAC #include <winperf.h> #include <winsock.h> #endif
In fact, it is easy to understand that files contain each other, and the definition order leads to the inclusion of Winsock before Winsock 2, which will lead to a large number of repeated definition errors.
Solution:
Find the code that contains windows.h and Winsock2.h, let Winsock2 be included before windows
(or write a header file to solve this problem)
In StdAfx.h add:
#include <WinSock2.h> #include <windows.h>
It can be solved.
If it is found that the solution does not contain these two files, it is possible that the content is used in the development package or other files.
Similar Posts:
- [Solved] Makefile xxx recipe for target xxx failed
- C1189 #error: “No Target Architecture”
- This paper solves the problem that the program published by QT prompts “unable to locate the program input point k32getmodulefilenameexa on the dynamic link library Kernel32. DLL” in XP environment
- About the compilation error “dereferencing pointer to incomplete type
- fatal error C1010: unexpected end of file while…
- PHP Fatal error: Cannot redeclare class
- Cannot find bounds of current function
- error: invalid storage class for function
- [Solved] Cannot Open “gl/glut.h” File Error: No such file or directory
- [Solved] VS error C4996: ‘scanf’: This function or variable may be unsafe.