问题描述
我正在用Visual C ++ 2010编译一个项目,但是某些Winsock重新定义存在问题.
I am compiling a project in Visual C++ 2010, but I have problems with some Winsock redefinitions.
首先我得到:
syntax error : identifier 'SOCKADDR_STORAGE'
但是,如果我包含winsock或winsock2或ws2tcpip,则会出现许多类似以下错误:
But if I include winsock or winsock2 or ws2tcpip i get many errors like these:
error C2011: 'sockaddr' : 'struct' type redefinition
error C2011: 'WSAData' : 'struct' type redefinition
error C2011: 'linger' : 'struct' type redefinition
推荐答案
您的问题是,通过包含Windows.h
,您也已经包含了winsock.h
.这是您的问题出现的原因,因为包含winsock2.h
或ws2tcpip.h
会尝试重新定义winsock.h
Your problem is that by including Windows.h
, you are also already including winsock.h
. It is here your problem arises as including winsock2.h
or ws2tcpip.h
will attempt to redefine some of the definitions in winsock.h
在包含Windows.h
之前使用#define WIN32_LEAN_AND_MEAN
,可以阻止编译器包含Windows.h
附带的很多额外内容
By using #define WIN32_LEAN_AND_MEAN
before your Windows.h
include you stop the compiler from including a lot of the extra stuff that comes with Windows.h
这篇关于Winsock重新定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!