10

I am compiling a project in Visual C++ 2010, but I have problems with some Winsock redefinitions.

First of all I get:

syntax error : identifier 'SOCKADDR_STORAGE'

But if I include winsock or winsock2 or ws2tcpip i get many errors like these:

error C2011: 'sockaddr' : 'struct' type redefinition 
error C2011: 'WSAData' : 'struct' type redefinition
error C2011: 'linger' : 'struct' type redefinition
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Cooker
  • 421
  • 1
  • 9
  • 19

1 Answers1

24

Your problem is that by including Windows.h, you are also already including winsock.h. It is here your problem arises as including winsock2.h or ws2tcpip.h will attempt to redefine some of the definitions in winsock.h

By using #define WIN32_LEAN_AND_MEAN before your Windows.h include you stop the compiler from including a lot of the extra stuff that comes with Windows.h

ctor
  • 5,938
  • 2
  • 26
  • 37
  • 1
    After spending a ton of time trying to solve this issue, your explanation is the one that not only worked but also made complete sense. Thanks! – Hey'Youssef Nov 27 '19 at 00:33