Tag Archives: ws2def. h

[Solved] VS winsock.h and ws2def.h a large number of redefined error

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.