Conflicting winsock.h and winsock2.h

This is a discussion on Conflicting winsock.h and winsock2.h within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I've got this situation here: My application uses a static lib that in turn includes winsock2.h. It worked fine ...

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    Conflicting winsock.h and winsock2.h

    Hi, I've got this situation here:

    My application uses a static lib that in turn includes winsock2.h. It worked fine until I've got some adjustment on the main application to include the windows.h. Now all of these errors came out:

    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(91) : warning C4005: 'AF_IPX' : macro redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(460) : see previous definition of 'AF_IPX'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(131) : warning C4005: 'AF_MAX' : macro redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(479) : see previous definition of 'AF_MAX'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(168) : warning C4005: 'SO_DONTLINGER' : macro redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(402) : see previous definition of 'SO_DONTLINGER'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(212) : error C2011: 'sockaddr' : 'struct' type redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(485) : see declaration of 'sockaddr'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(390) : error C2143: syntax error : missing '}' before 'constant'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(390) : error C2143: syntax error : missing ';' before 'constant'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(390) : error C2059: syntax error : 'constant'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(443) : error C2143: syntax error : missing ';' before '}'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(443) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(443) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(524) : warning C4005: 'IN_CLASSA' : macro redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(287) : see previous definition of 'IN_CLASSA'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(530) : warning C4005: 'IN_CLASSB' : macro redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(293) : see previous definition of 'IN_CLASSB'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(536) : warning C4005: 'IN_CLASSC' : macro redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(299) : see previous definition of 'IN_CLASSC'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(547) : warning C4005: 'INADDR_ANY' : macro redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(304) : see previous definition of 'INADDR_ANY'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(549) : warning C4005: 'INADDR_BROADCAST' : macro redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(306) : see previous definition of 'INADDR_BROADCAST'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(583) : error C2011: 'sockaddr_in' : 'struct' type redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(312) : see declaration of 'sockaddr_in'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(132) : error C2011: 'fd_set' : 'struct' type redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(68) : see declaration of 'fd_set'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(167) : warning C4005: 'FD_SET' : macro redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(102) : see previous definition of 'FD_SET'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(176) : error C2011: 'timeval' : 'struct' type redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(111) : see declaration of 'timeval'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(232) : error C2011: 'hostent' : 'struct' type redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(167) : see declaration of 'hostent'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(245) : error C2011: 'netent' : 'struct' type redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(180) : see declaration of 'netent'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(252) : error C2011: 'servent' : 'struct' type redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(187) : see declaration of 'servent'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(264) : error C2011: 'protoent' : 'struct' type redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(199) : see declaration of 'protoent'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(360) : error C2011: 'WSAData' : 'struct' type redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(322) : see declaration of 'WSAData'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(458) : error C2011: 'sockproto' : 'struct' type redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(494) : see declaration of 'sockproto'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(500) : error C2011: 'linger' : 'struct' type redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(531) : see declaration of 'linger'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(513) : warning C4005: 'SOMAXCONN' : macro redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(544) : see previous definition of 'SOMAXCONN'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(542) : warning C4005: 'FD_READ' : macro redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(562) : see previous definition of 'FD_READ'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(545) : warning C4005: 'FD_WRITE' : macro redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(563) : see previous definition of 'FD_WRITE'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(548) : warning C4005: 'FD_OOB' : macro redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(564) : see previous definition of 'FD_OOB'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(551) : warning C4005: 'FD_ACCEPT' : macro redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(565) : see previous definition of 'FD_ACCEPT'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(554) : warning C4005: 'FD_CONNECT' : macro redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(566) : see previous definition of 'FD_CONNECT'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(557) : warning C4005: 'FD_CLOSE' : macro redefinition
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(567) : see previous definition of 'FD_CLOSE'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(1578) : error C2375: 'accept' : redefinition; different linkage
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(742) : see declaration of 'accept'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(1600) : error C2375: 'bind' : redefinition; different linkage
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(747) : see declaration of 'bind'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(1619) : error C2375: 'closesocket' : redefinition; different linkage
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(752) : see declaration of 'closesocket'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(1638) : error C2375: 'connect' : redefinition; different linkage
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(754) : see declaration of 'connect'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(1659) : error C2375: 'ioctlsocket' : redefinition; different linkage
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(759) : see declaration of 'ioctlsocket'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(1680) : error C2375: 'getpeername' : redefinition; different linkage
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(764) : see declaration of 'getpeername'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(1701) : error C2375: 'getsockname' : redefinition; different linkage
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(769) : see declaration of 'getsockname'
    3>C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock2.h(1724) : error C2375: 'getsockopt' : redefinition; different linkage
    3> C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(774) : see declaration of 'getsockopt'
    ...
    Now I've searched the web and they said to define WIN_LEAN_AND_MEAN. I've tried that and it still didn't work. I also tried to define WIN_LEAN_AND_MEAN in the static lib, and the library itself can't be compiled. So, can anyone help me? Thanks in advance.
    Last edited by g4j31a5; 12-28-2010 at 01:19 AM.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) winsock2 supercedes winsock.

    2) For reasons that make no sense to me, you have to include winsock2.h before windows.h

    3) You probably need to use include guards in your header files to prevent including the same delarations more than once...
    Code:
    #ifndef HEADER_FILE_NAME
    #define HEADER_FILE_NAME
    
    #include <winsock2.h>
    #include <windows.h>  // yes the order matters
    
    
    // body of declarations
    
    #endif //HEADER_FILE_NAME
    4) The correct define is WIN32_LEAN_AND_MEAN which probably wouldn't help you in this situation.
    Last edited by CommonTater; 12-28-2010 at 06:13 AM.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by CommonTater View Post
    2) For reasons that make no sense to me, you have to include winsock2.h before windows.h
    It's because windows.h unfortunately includes winsock.h.

    The easiest way to get around this problem is to not include windows.h in your code anywhere. winsock2.h will automatically include windows.h for you.
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,262
    I'm at pains to quote Gary Coleman here, seeing as none of his shows were shown in the UK (I did see him in the Simpsons once), but... "What'choo talkin' 'bout, everybody?"

    Clearing the FUD here:-

    1. windows.h includes the entire Windows API by default;
    2. defining WIN32_LEAN_AND_MEAN before it prevents all but the core APIs (kernel32, user, gdi, a few others) from being processed;
    3. doing point 2 will allow you to manually include winsock2.h and thus solve your problem.

  5. #5
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    First, my bad what I meant was indeed WIN32_LEAN_AND_MEAN. :P

    Second, like SMurf said, if WIN32_LEAN_AND_MEAN, winsock.h won't be included.

    Thirdly, I've solved my problem. ^_^ Actually I've got 3 libraries that I am using. Delta 3D, Jetbyte Tools for the Socket Programming, and Logitech SDK for the controller. At first, the Logitech SDK is not being used so it's fine. But now with the SDK, I have to include windows.h. My fault was I defined WIN32_LEAN_AND_MEAN on both libraries, the Jetbyte Tools and Logitech SDK. But after I analyzed it thoroughly, it was because I'm also including a header from OSG, one of the dependencies of Delta 3D. And in that header there's no definition of WIN32_LEAN_AND_MEAN at all (and thus the winsock.h was included). So I decided to define it above my main function instead.

    Thanks a lot tho guys. Sorry for the inconvenience.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  6. #6
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Double post. Sorry...
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-04-2009, 01:57 PM
  2. winsock2.h problem
    By xixpsychoxix in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-08-2008, 05:08 PM
  3. Winsock.h and Winsock2.h
    By azteched in forum Networking/Device Communication
    Replies: 5
    Last Post: 12-24-2004, 06:42 PM
  4. winsock2.h
    By Carp in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2003, 01:42 PM
  5. Errors when including winsock2.h
    By skiingwiz in forum Windows Programming
    Replies: 2
    Last Post: 12-27-2002, 06:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21