2012-07-30 79 views
6

我無法在Win32.h中包含msvC++ 2010項目。起初,雖然這是我做錯了,所以我創建了一個空的項目來測試我的理智。空項目如下無法在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 ========== 

我假設微軟不會在他們的SDK發佈壞頭文件,這是怎麼回事?

+1

重複的[WinSock2.h中的重定義錯誤](http://stackoverflow.com/questions/5971332/redefinition-errors-in-winsock2-h) – 2012-07-30 18:04:38

回答

24

我相信你會得到這些錯誤,因爲windows.h將包含winsock.h。顛倒你的包含的順序,使WinSock2.h在windows.h之前。 WinSock2.h有一些#defines來保持windows.h不包括winsock.h

+4

爲了詳細說明這一點,winsock2.h應該是一個替換winsock.h,它們並不意味着一起使用。如果首先包含winsock.h,則winsock2.h將無法編譯,因爲它會重新聲明winsock.h已聲明的所有內容。如果首先包含winsock2.h,則會禁用winsock.h。如果'_WIN32_WINNT> = 0x0400',windows.h包含winsock2.h,否則它包含winsock.h。 – 2012-07-30 21:04:26

+2

英雄!這已經讓我堅持了30分鐘。 – connorbode 2014-01-29 22:43:11

0

我確保#include「Winsock2.h」位於任何#include「windows.h」和「#include」之前WINSOCK.H」,並解決了這個情況。

耐心早晚的事情,看看包括一個接一個,建立這個順序,首先的#include‘Winsock2.h’,然後#包括‘WINDOWS.H’

我檢查了遞歸包含,我發現了包含(遞歸)一些#include「windows.h」和「#include」Winsock.h「的頭文件,並編寫了一個#include」Winsock2.h「。在這個文件中,我添加了#包含「Winsock2.h」作爲第一個包含。

相關問題