I'm having a problem including both files. Now, I know I need to either include Winsock2 first, then windows.h, or simply put:
#define WIN32_LEAN_AND_MEAN
but, I'm still having problems
I have a header file that is called XS.h
which looks like this
#ifndef XS_H
#define XS_H
#include <winsock2.h>
#include <ws2tcpip.h>
#include <Windows.h>
#endif
and I'm including XS.h
in the header Client.h
.
Client.h
include is looks like this :
#ifndef CLIENT_H
#define CLIENT_H
#include "XS.h"
XS.h
is my only include in Client.h
, yet I still get errors (and as you can see, Winsock
is included before windows.h
I'm getting about 78 errors, here are some of them :
Error 90 error C3861: 'WSASetLastError': identifier not found c:\program files (x86)\windows kits\8.0\include\um\ws2tcpip.h 703
Error 61 error C2375: 'WSAStartup' : redefinition; different linkage c:\program files (x86)\windows kits\8.0\include\um\winsock2.h 2296
Error 49 error C2375: 'send' : redefinition; different linkage c:\program files (x86)\windows kits\8.0\include\um\winsock2.h 2026
How can I solve this issue?
Thanks!
Edit: I've tried to use #define _WINSOCKAPI_
as well, though it did not resolve my problems...
I have winsock.h
first, then windows.h
, though it still does the error for me.
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: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 onWIN32_LEAN_AND_MEAN
optimization for correctness.I.e., minimum:
I made sure that an
#include "Winsock2.h"
is before any#include "windows.h"
and"#include "Winsock.h"
and this solved the case.Just a matter of patience, look at includes one by one and establish this order, first
#include "Winsock2.h"
then#include "windows.h"
I checked the recursive includes, I spotted the header files which include (recursively) some #include "windows.h" and "#include "Winsock.h"and write a#include "Winsock2.h". in this files, i added#include "Winsock2.h"` as the first include.