Winpcap 程序与 MinGW 一起运行,但在 Qt 中不与 MSVC 2015 一起运行
Winpcap program runs with MinGW but not with MSVC 2015 in Qt
我有一个使用 winpcap 库的 Qt 应用程序。当我使用 MinGW 64 位构建项目时,一切正常,项目 运行s 没有任何错误。但是,当我使用 MSVC 2015 64 位编译同一个应用程序时,我遇到了很多与重新定义、不同链接或宏重新定义相关的错误。我还有其他使用 winpcap 的 Qt 应用程序 运行 在同一个 OS(windows 10 64 位)上,均使用 MinGW 和 MSVC 2015。
部分错误如下
C:\Program Files (x86)\Windows Kits\include.0.14393.0\shared\ws2def.h(103): warning C4005: 'AF_IPX': macro redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(457): note: see previous definition of 'AF_IPX'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\shared\ws2def.h(147): warning C4005: 'AF_MAX': macro redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(476): note: see previous definition of 'AF_MAX'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\shared\ws2def.h(185): warning C4005: 'SO_DONTLINGER': macro redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(399): note: see previous definition of 'SO_DONTLINGER'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\shared\ws2def.h(235): error C2011: 'sockaddr': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(1007): note: see declaration of 'sockaddr'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\shared\ws2def.h(437): error C2059: syntax error: 'constant'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\shared\ws2def.h(437): error C3805: 'constant': unexpected token, expected either '}' or a ','
C:\Program Files (x86)\Windows Kits\include.0.14393.0\shared\ws2def.h(572): warning C4005: 'IN_CLASSA': macro redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(284): note: see previous definition of 'IN_CLASSA'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\shared\ws2def.h(597): warning C4005: 'INADDR_BROADCAST': macro redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(303): note: see previous definition of 'INADDR_BROADCAST'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\shared\ws2def.h(633): error C2011: 'sockaddr_in': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(1011): note: see declaration of 'sockaddr_in'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(136): error C2011: 'fd_set': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(1019): note: see declaration of 'fd_set'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(156): warning C4005: 'FD_CLR': macro redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(94): note: see previous definition of 'FD_CLR'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(171): warning C4005: 'FD_SET': macro redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(99): note: see previous definition of 'FD_SET'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(180): error C2011: 'timeval': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(1035): note: see declaration of 'timeval'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(236): error C2011: 'hostent': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(1023): note: see declaration of 'hostent'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(249): error C2011: 'netent': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(177): note: see declaration of 'netent'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(256): error C2011: 'servent': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(1027): note: see declaration of 'servent'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(268): error C2011: 'protoent': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(1031): note: see declaration of 'protoent'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(364): error C2011: 'WSAData': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(319): note: see declaration of 'WSAData'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(462): error C2011: 'sockproto': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(491): note: see declaration of 'sockproto'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(504): error C2011: 'linger': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(1015): note: see declaration of 'linger'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(517): warning C4005: 'SOMAXCONN': macro redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(541): note: see previous definition of 'SOMAXCONN'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(1667): error C2375: 'closesocket': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(749): note: see declaration of 'closesocket'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(1684): error C2375: 'connect': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(751): note: see declaration of 'connect'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(1705): error C2375: 'ioctlsocket': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(756): note: see declaration of 'ioctlsocket'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(1726): error C2375: 'getpeername': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(761): note: see declaration of 'getpeername'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(1747): error C2375: 'getsockname': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(766): note: see declaration of 'getsockname'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(1768): error C2375: 'getsockopt': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(771): note: see declaration of 'getsockopt'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(1793): error C2375: 'htonl': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(778): note: see declaration of 'htonl'
请注意,我已经在 "windows.h" 之前包含了 "winsock2.h" ,这是我在 Whosebug 上发现的许多相关问题的解决方案,例如 this one and this one 。我也试过定义 WIN32_LEAN_AND_MEAN 确实减少了一些错误。任何帮助,将不胜感激。谢谢
正如 Whosebug 上类似问题所建议的那样,问题在于在“winsock2.h”之前包含“windows.h”。我实际上拥有的是一个名为 mypcap.h 的 header 文件和另一个名为 mainwindow.h[=26 的 header 文件=] 包括 mypcap.h.
mypcap.h:
#pragma once
#include "winsock2.h"
#include "windows.h"
// ...
mainwindow.h:
#pragma once
/*
---- Includes ---
*/
#include "mypcap.h"
在 mainwindow.h 中,windows.h 已包含在其他 header 中。在所有包含之前放置 #define _WINSOCKAPI_
修复了错误。将 mypcap.h 移动到 mainwindow.h 的顶部是另一种解决方案。
我有一个使用 winpcap 库的 Qt 应用程序。当我使用 MinGW 64 位构建项目时,一切正常,项目 运行s 没有任何错误。但是,当我使用 MSVC 2015 64 位编译同一个应用程序时,我遇到了很多与重新定义、不同链接或宏重新定义相关的错误。我还有其他使用 winpcap 的 Qt 应用程序 运行 在同一个 OS(windows 10 64 位)上,均使用 MinGW 和 MSVC 2015。 部分错误如下
C:\Program Files (x86)\Windows Kits\include.0.14393.0\shared\ws2def.h(103): warning C4005: 'AF_IPX': macro redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(457): note: see previous definition of 'AF_IPX'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\shared\ws2def.h(147): warning C4005: 'AF_MAX': macro redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(476): note: see previous definition of 'AF_MAX'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\shared\ws2def.h(185): warning C4005: 'SO_DONTLINGER': macro redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(399): note: see previous definition of 'SO_DONTLINGER'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\shared\ws2def.h(235): error C2011: 'sockaddr': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(1007): note: see declaration of 'sockaddr'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\shared\ws2def.h(437): error C2059: syntax error: 'constant'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\shared\ws2def.h(437): error C3805: 'constant': unexpected token, expected either '}' or a ','
C:\Program Files (x86)\Windows Kits\include.0.14393.0\shared\ws2def.h(572): warning C4005: 'IN_CLASSA': macro redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(284): note: see previous definition of 'IN_CLASSA'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\shared\ws2def.h(597): warning C4005: 'INADDR_BROADCAST': macro redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(303): note: see previous definition of 'INADDR_BROADCAST'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\shared\ws2def.h(633): error C2011: 'sockaddr_in': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(1011): note: see declaration of 'sockaddr_in'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(136): error C2011: 'fd_set': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(1019): note: see declaration of 'fd_set'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(156): warning C4005: 'FD_CLR': macro redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(94): note: see previous definition of 'FD_CLR'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(171): warning C4005: 'FD_SET': macro redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(99): note: see previous definition of 'FD_SET'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(180): error C2011: 'timeval': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(1035): note: see declaration of 'timeval'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(236): error C2011: 'hostent': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(1023): note: see declaration of 'hostent'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(249): error C2011: 'netent': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(177): note: see declaration of 'netent'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(256): error C2011: 'servent': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(1027): note: see declaration of 'servent'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(268): error C2011: 'protoent': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(1031): note: see declaration of 'protoent'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(364): error C2011: 'WSAData': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(319): note: see declaration of 'WSAData'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(462): error C2011: 'sockproto': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(491): note: see declaration of 'sockproto'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(504): error C2011: 'linger': 'struct' type redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(1015): note: see declaration of 'linger'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(517): warning C4005: 'SOMAXCONN': macro redefinition
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(541): note: see previous definition of 'SOMAXCONN'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(1667): error C2375: 'closesocket': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(749): note: see declaration of 'closesocket'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(1684): error C2375: 'connect': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(751): note: see declaration of 'connect'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(1705): error C2375: 'ioctlsocket': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(756): note: see declaration of 'ioctlsocket'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(1726): error C2375: 'getpeername': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(761): note: see declaration of 'getpeername'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(1747): error C2375: 'getsockname': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(766): note: see declaration of 'getsockname'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(1768): error C2375: 'getsockopt': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(771): note: see declaration of 'getsockopt'
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock2.h(1793): error C2375: 'htonl': redefinition; different linkage
C:\Program Files (x86)\Windows Kits\include.0.14393.0\um\winsock.h(778): note: see declaration of 'htonl'
请注意,我已经在 "windows.h" 之前包含了 "winsock2.h" ,这是我在 Whosebug 上发现的许多相关问题的解决方案,例如 this one and this one 。我也试过定义 WIN32_LEAN_AND_MEAN 确实减少了一些错误。任何帮助,将不胜感激。谢谢
正如 Whosebug 上类似问题所建议的那样,问题在于在“winsock2.h”之前包含“windows.h”。我实际上拥有的是一个名为 mypcap.h 的 header 文件和另一个名为 mainwindow.h[=26 的 header 文件=] 包括 mypcap.h.
mypcap.h:
#pragma once
#include "winsock2.h"
#include "windows.h"
// ...
mainwindow.h:
#pragma once
/*
---- Includes ---
*/
#include "mypcap.h"
在 mainwindow.h 中,windows.h 已包含在其他 header 中。在所有包含之前放置 #define _WINSOCKAPI_
修复了错误。将 mypcap.h 移动到 mainwindow.h 的顶部是另一种解决方案。