Make sure that <windows.h>
doesn't include <winsock.h>
(which provides many of the same declarations as <winsock2.h>
). In the <winsock2.h>
file on my system there is this line:
#define _WINSOCKAPI_ /* Prevent inclusion of winsock.h in windows.h */
The _WINSOCKAPI_
include guard may be an internal implementation detail, but as a practical solution I would rely on it, just defining this symbol before including <windows.h>
, e.g. in the compiler invocation (which for an IDE means in the IDE project settings).
Alternatively you can try to always include <winsock2.h>
before <windows.h>
, in order to establish the relevant include guard whatever it is (but this is I think much more fragile than just assuming that the above guard is practically well-defined);
or you can define WIN32_LEAN_AND_MEAN
, which prevents <windows.h>
from including <winsock.h>
but also some other headers (listing from source on my system those are <cderr.h>
, <dde.h>
, <ddeml.h>
, <dlgs.h>
, <lzexpand.h>
, <mmsystem.h>
, <nb30.h>
, <rpc.h>
, <shellapi.h>
, <winperf.h>
, <wincrypt.h>
, <winefs.h>
, <winscard.h>
, <winspool.h>
, <ole2.h>
, and <commdlg.h>
). I do not recommend relying on WIN32_LEAN_AND_MEAN
optimization for correctness.
I.e., minimum:
#undef UNICODE
#define UNICODE
#undef _WINSOCKAPI_
#define _WINSOCKAPI_
#include <windows.h>
#include <winsock2.h>
auto main()
-> int
{}