14

msvc++2010 プロジェクトに WinSock2.h を含めることができません。最初は自分のやり方が間違っていると思ったので、自分の正気をテストするために空のプロジェクトを作成しました。空のプロジェクトは次のとおりです

#include <Windows.h>
#include <WinSock2.h>


int main(void){

    system("echo Hello World");
    system("pause");
    return 0;

};

行がなくてもコンパイルして正常に動作し#include <WinSock2.h>ますが、WinSock2.h をインクルードしようとすると、長いエラー リストで失敗します。

エラー:

1>  Main.cpp
1>c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(91): warning C4005: 'AF_IPX' : macro redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(460) : see previous definition of 'AF_IPX'
1>c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(131): warning C4005: 'AF_MAX' : macro redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(479) : see previous definition of 'AF_MAX'
1>c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(168): warning C4005: 'SO_DONTLINGER' : macro redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(402) : see previous definition of 'SO_DONTLINGER'
1>c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(212): error C2011: 'sockaddr' : 'struct' type redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(485) : see declaration of 'sockaddr'
1>c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(390): error C2059: syntax error : 'constant'
1>c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(390): error C3805: 'constant': unexpected token, expected either '}' or a ','
1>c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(524): warning C4005: 'IN_CLASSA' : macro redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(287) : see previous definition of 'IN_CLASSA'
1>c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(530): warning C4005: 'IN_CLASSB' : macro redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(293) : see previous definition of 'IN_CLASSB'
1>c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(536): warning C4005: 'IN_CLASSC' : macro redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(299) : see previous definition of 'IN_CLASSC'
1>c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(547): warning C4005: 'INADDR_ANY' : macro redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(304) : see previous definition of 'INADDR_ANY'
1>c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(549): warning C4005: 'INADDR_BROADCAST' : macro redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(306) : see previous definition of 'INADDR_BROADCAST'
1>c:\program files\microsoft sdks\windows\v7.0a\include\ws2def.h(583): error C2011: 'sockaddr_in' : 'struct' type redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(312) : see declaration of 'sockaddr_in'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(132): error C2011: 'fd_set' : 'struct' type redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(68) : see declaration of 'fd_set'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(167): warning C4005: 'FD_SET' : macro redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(102) : see previous definition of 'FD_SET'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(176): error C2011: 'timeval' : 'struct' type redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(111) : see declaration of 'timeval'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(232): error C2011: 'hostent' : 'struct' type redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(167) : see declaration of 'hostent'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(245): error C2011: 'netent' : 'struct' type redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(180) : see declaration of 'netent'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(252): error C2011: 'servent' : 'struct' type redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(187) : see declaration of 'servent'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(264): error C2011: 'protoent' : 'struct' type redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(199) : see declaration of 'protoent'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(360): error C2011: 'WSAData' : 'struct' type redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(322) : see declaration of 'WSAData'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(458): error C2011: 'sockproto' : 'struct' type redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(494) : see declaration of 'sockproto'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(500): error C2011: 'linger' : 'struct' type redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(531) : see declaration of 'linger'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(513): warning C4005: 'SOMAXCONN' : macro redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(544) : see previous definition of 'SOMAXCONN'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(542): warning C4005: 'FD_READ' : macro redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(562) : see previous definition of 'FD_READ'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(545): warning C4005: 'FD_WRITE' : macro redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(563) : see previous definition of 'FD_WRITE'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(548): warning C4005: 'FD_OOB' : macro redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(564) : see previous definition of 'FD_OOB'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(551): warning C4005: 'FD_ACCEPT' : macro redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(565) : see previous definition of 'FD_ACCEPT'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(554): warning C4005: 'FD_CONNECT' : macro redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(566) : see previous definition of 'FD_CONNECT'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(557): warning C4005: 'FD_CLOSE' : macro redefinition
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(567) : see previous definition of 'FD_CLOSE'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1578): error C2375: 'accept' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(742) : see declaration of 'accept'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1600): error C2375: 'bind' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(747) : see declaration of 'bind'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1619): error C2375: 'closesocket' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(752) : see declaration of 'closesocket'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1638): error C2375: 'connect' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(754) : see declaration of 'connect'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1659): error C2375: 'ioctlsocket' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(759) : see declaration of 'ioctlsocket'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1680): error C2375: 'getpeername' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(764) : see declaration of 'getpeername'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1701): error C2375: 'getsockname' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(769) : see declaration of 'getsockname'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1724): error C2375: 'getsockopt' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(774) : see declaration of 'getsockopt'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1745): error C2375: 'htonl' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(781) : see declaration of 'htonl'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1762): error C2375: 'htons' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(783) : see declaration of 'htons'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1780): error C2375: 'inet_addr' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(785) : see declaration of 'inet_addr'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1797): error C2375: 'inet_ntoa' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(787) : see declaration of 'inet_ntoa'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1815): error C2375: 'listen' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(789) : see declaration of 'listen'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1833): error C2375: 'ntohl' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(793) : see declaration of 'ntohl'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1850): error C2375: 'ntohs' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(795) : see declaration of 'ntohs'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1870): error C2375: 'recv' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(797) : see declaration of 'recv'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1895): error C2375: 'recvfrom' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(803) : see declaration of 'recvfrom'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1921): error C2375: 'select' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(811) : see declaration of 'select'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1945): error C2375: 'send' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(818) : see declaration of 'send'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1970): error C2375: 'sendto' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(824) : see declaration of 'sendto'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(1996): error C2375: 'setsockopt' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(832) : see declaration of 'setsockopt'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2018): error C2375: 'shutdown' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(839) : see declaration of 'shutdown'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2039): error C2375: 'socket' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(843) : see declaration of 'socket'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2063): error C2375: 'gethostbyaddr' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(850) : see declaration of 'gethostbyaddr'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2083): error C2375: 'gethostbyname' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(855) : see declaration of 'gethostbyname'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2101): error C2375: 'gethostname' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(857) : see declaration of 'gethostname'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2121): error C2375: 'getservbyport' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(861) : see declaration of 'getservbyport'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2141): error C2375: 'getservbyname' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(865) : see declaration of 'getservbyname'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2160): error C2375: 'getprotobynumber' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(869) : see declaration of 'getprotobynumber'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2178): error C2375: 'getprotobyname' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(871) : see declaration of 'getprotobyname'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2200): error C2375: 'WSAStartup' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(875) : see declaration of 'WSAStartup'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2219): error C2375: 'WSACleanup' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(879) : see declaration of 'WSACleanup'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2236): error C2375: 'WSASetLastError' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(881) : see declaration of 'WSASetLastError'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2253): error C2375: 'WSAGetLastError' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(883) : see declaration of 'WSAGetLastError'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2270): error C2375: 'WSAIsBlocking' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(885) : see declaration of 'WSAIsBlocking'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2287): error C2375: 'WSAUnhookBlockingHook' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(887) : see declaration of 'WSAUnhookBlockingHook'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2304): error C2375: 'WSASetBlockingHook' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(889) : see declaration of 'WSASetBlockingHook'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2321): error C2375: 'WSACancelBlockingCall' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(891) : see declaration of 'WSACancelBlockingCall'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2343): error C2375: 'WSAAsyncGetServByName' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(893) : see declaration of 'WSAAsyncGetServByName'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2370): error C2375: 'WSAAsyncGetServByPort' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(901) : see declaration of 'WSAAsyncGetServByPort'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2396): error C2375: 'WSAAsyncGetProtoByName' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(909) : see declaration of 'WSAAsyncGetProtoByName'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2421): error C2375: 'WSAAsyncGetProtoByNumber' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(916) : see declaration of 'WSAAsyncGetProtoByNumber'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2446): error C2375: 'WSAAsyncGetHostByName' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(923) : see declaration of 'WSAAsyncGetHostByName'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2473): error C2375: 'WSAAsyncGetHostByAddr' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(930) : see declaration of 'WSAAsyncGetHostByAddr'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2496): error C2375: 'WSACancelAsyncRequest' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(939) : see declaration of 'WSACancelAsyncRequest'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winsock2.h(2517): error C2375: 'WSAAsyncSelect' : redefinition; different linkage
1>          c:\program files\microsoft sdks\windows\v7.0a\include\winsock.h(941) : see declaration of 'WSAAsyncSelect'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Microsoft は SDK で不正なヘッダー ファイルを配布しないと思いますが、何が起こっているのでしょうか?

4

2 に答える 2

39

windows.h には winsock.h が含まれているため、これらのエラーが発生していると思います。インクルードの順序を逆にして、WinSock2.h が wi​​ndows.h の前になるようにします。WinSock2.h には、windows.h に winsock.h が含まれないようにするための #define が含まれています。

于 2012-07-30T18:05:05.813 に答える
4

#include "Winsock2.h" が #include "windows.h" および "#include "Winsock.h" の前にあることを確認し、これで問題が解決しました。

辛抱強く、インクルードを 1 つずつ見て、この順序を確立します。最初に #include "Winsock2.h"、次に #include "windows.h"

再帰的なインクルードを確認し、#include "windows.h" と "#include "Winsock.h" を (再帰的に) インクルードするヘッダー ファイルを見つけ、#include "Winsock2.h" を書き込みます。このファイルに、追加しました#include "Winsock2.h"` を最初のインクルードとして使用します。

于 2014-08-25T18:00:18.553 に答える