42

Including winsock2.h, in a file of a project.

Getting many errors in -
WinSock2.h(109) : error C2011: 'fd_set' : 'struct' type redefinition
with a corresponding -
WinSock.h(54) : see declaration of 'fd_set'

I don't see winsock.h included in any of the headers.

Are there any setting in the project that may be causing this?

EDIT
However, I am using windows.h in another file:
http://cboard.cprogramming.com/windows-programming/41212-strange-msvc-winsock2-compile-error.html

EDIT 2
In the header I have:

#include <winsock2.h>  
#include <iphlpapi.h>
#include "\MyFiles\FileX.h" <-which #include <windows.h> 
T.T.T.
  • 33,367
  • 47
  • 130
  • 168

2 Answers2

69

My educated guess would be the order of included headers, i.e. include winsock2.h first (with first meaning before windows.h), or define WIN32_LEAN_AND_MEAN before including windows.h, which prevents windows.h from including winsock v1.

#include <winsock2.h>
#include <windows.h>

-or-

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
Jim Brissom
  • 31,821
  • 4
  • 39
  • 33
  • Jim, I would think the way I included my other header file, winsock2.h would be first? Do I have to put the winsock2 in the other file? (see EDIT 2) – T.T.T. May 11 '11 at 22:36
  • 2
    Wish they would just fix this forever in visual studio! Thanks for explaining the errors. – hookenz Dec 04 '12 at 03:02
  • 54
    I'm amazed how much Windows API sucks. – Bartek Banachewicz Dec 11 '12 at 12:44
  • @Bartek: I agree. Not exactly how much the API itself sucks, but the SDK. – alecov Mar 15 '13 at 15:22
  • 4
    +1 WIN32_LEAN_AND_MEAN saved my day :) – Wolf Apr 01 '14 at 09:45
  • In Qt, I had multiple calls to windows.h from other 3rd party source code and only one call to winsock2.h in some new 3rd party includes. I wound up having to put DEFINES += WIN32_LEAN_AND_MEAN in my .pro file to fix this. Thanks +1 – UndeadBob Aug 01 '14 at 19:17
  • if you happen to include `stdafx.h` all over the place, you can place `#include ` at the begining of it. i took over some code that happen to fall under this case, and this is what saved my life – Hame Dec 09 '14 at 08:05
  • In my case, I am just including WS2tcpip.h everywhere (other 2 files are not included, because they are internally included in this one).But still I am getting redefinition error for many functions. Most of these functions are declared under #if INCL_WINSOCK_API_PROTOTYPES block which is defined to 1. Can you suggest something on this? Thanks for the help! – Shivaraj Bhat Jan 22 '15 at 06:50
5

After some headache: I made sure that an #include "Winsock2.h" is before any #include "windows.h" and "#include "Winsock.h" and this solved the case.

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.

Just a matter of patience, look at includes one by one and establish this order, first #include "Winsock2.h" then #include "windows.h"

Igor Skochinsky
  • 24,629
  • 2
  • 72
  • 109
kiriloff
  • 25,609
  • 37
  • 148
  • 229