Search code examples
c++visual-studioqtvisual-c++winpcap

Winpcap program runs with MinGW but not with MSVC 2015 in Qt


I have a Qt application that uses winpcap library. When I build the project with MinGW 64-bit, everything works fine and the project runs without any errors. But when I compile the same application with MSVC 2015 64-bit, I get a lot of errors related to redefinition, different linkage or macro redefinition. I have other Qt applications using winpcap that run on the same OS (windows 10 64-bit), both with MinGW and MSVC 2015. Part of the errors are as below.

C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\shared\ws2def.h(103): warning C4005: 'AF_IPX': macro redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(457): note: see previous definition of 'AF_IPX'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\shared\ws2def.h(147): warning C4005: 'AF_MAX': macro redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(476): note: see previous definition of 'AF_MAX'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\shared\ws2def.h(185): warning C4005: 'SO_DONTLINGER': macro redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(399): note: see previous definition of 'SO_DONTLINGER'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\shared\ws2def.h(235): error C2011: 'sockaddr': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(1007): note: see declaration of 'sockaddr'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\shared\ws2def.h(437): error C2059: syntax error: 'constant'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\shared\ws2def.h(437): error C3805: 'constant': unexpected token, expected either '}' or a ','
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\shared\ws2def.h(572): warning C4005: 'IN_CLASSA': macro redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(284): note: see previous definition of 'IN_CLASSA'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\shared\ws2def.h(597): warning C4005: 'INADDR_BROADCAST': macro redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(303): note: see previous definition of 'INADDR_BROADCAST'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\shared\ws2def.h(633): error C2011: 'sockaddr_in': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(1011): note: see declaration of 'sockaddr_in'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(136): error C2011: 'fd_set': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(1019): note: see declaration of 'fd_set'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(156): warning C4005: 'FD_CLR': macro redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(94): note: see previous definition of 'FD_CLR'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(171): warning C4005: 'FD_SET': macro redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(99): note: see previous definition of 'FD_SET'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(180): error C2011: 'timeval': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(1035): note: see declaration of 'timeval'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(236): error C2011: 'hostent': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(1023): note: see declaration of 'hostent'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(249): error C2011: 'netent': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(177): note: see declaration of 'netent'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(256): error C2011: 'servent': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(1027): note: see declaration of 'servent'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(268): error C2011: 'protoent': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(1031): note: see declaration of 'protoent'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(364): error C2011: 'WSAData': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(319): note: see declaration of 'WSAData'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(462): error C2011: 'sockproto': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(491): note: see declaration of 'sockproto'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(504): error C2011: 'linger': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(1015): note: see declaration of 'linger'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(517): warning C4005: 'SOMAXCONN': macro redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(541): note: see previous definition of 'SOMAXCONN'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(1667): error C2375: 'closesocket': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(749): note: see declaration of 'closesocket'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(1684): error C2375: 'connect': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(751): note: see declaration of 'connect'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(1705): error C2375: 'ioctlsocket': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(756): note: see declaration of 'ioctlsocket'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(1726): error C2375: 'getpeername': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(761): note: see declaration of 'getpeername'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(1747): error C2375: 'getsockname': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(766): note: see declaration of 'getsockname'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(1768): error C2375: 'getsockopt': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(771): note: see declaration of 'getsockopt'
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock2.h(1793): error C2375: 'htonl': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\um\winsock.h(778): note: see declaration of 'htonl'

Please note that I already have included "winsock2.h" before "windows.h" , which is the solution to a lot of related questions I found on stackoverflow, like this one and this one . I've also tried defining WIN32_LEAN_AND_MEAN which did reduce a few errors. Any help would be appreciated. Thanks.


Solution

  • As suggested by similar questions on StackOverflow, the problem is with including "windows.h" before "winsock2.h". What I actually had was a header file called mypcap.h and another header file called mainwindow.h that included mypcap.h.

    mypcap.h:

    #pragma once
    
    #include "winsock2.h"
    #include "windows.h"
    
    // ...
    

    mainwindow.h:

    #pragma once
    /*
    ---- Includes ---
    */
    #include "mypcap.h"
    

    In mainwindow.h, windows.h was already included in the other headers. Putting #define _WINSOCKAPI_ before all the includes fixed the errors. Moving mypcap.h to the top of mainwindow.h was another solution.