winsock编译错误
以下错误来自仅包含 windows 和 Winsock2 的文件。
C:\Users\ioil\Desktop\dm\bin>dmc sockit.c
typedef struct fd_set {
^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(85) : Error: 'fd_set' is already defined
} fd_set;
^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(88) : Error: identifier or '( declarator )' expected
struct timeval {
^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(129) : Error: 'timeval' is already defined
};
^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(132) : Error: identifier or '( declarator )' expected
struct hostent {
^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(185) : Error: 'hostent' is already defined
Fatal error: too many errors
--- errorlevel 1
C:\Users\ioil\Desktop\dm\bin>
已经尝试过的方法:将winsock.dll文件放在与编译器和要编译的程序相同的目录中,将其放在system32目录中,然后使用regsrv32命令将其输入注册表中。真的不知道从这里去哪里,感谢任何建议。 。 。
The following errors are from a file with just windows and winsock2 included.
C:\Users\ioil\Desktop\dm\bin>dmc sockit.c
typedef struct fd_set {
^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(85) : Error: 'fd_set' is already defined
} fd_set;
^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(88) : Error: identifier or '( declarator )' expected
struct timeval {
^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(129) : Error: 'timeval' is already defined
};
^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(132) : Error: identifier or '( declarator )' expected
struct hostent {
^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(185) : Error: 'hostent' is already defined
Fatal error: too many errors
--- errorlevel 1
C:\Users\ioil\Desktop\dm\bin>
What's already been tried : placing the winsock.dll file in the same directory as the compiler and program to be compiled, placing it in the system32 directory, and entering it in the registry with the regsrv32 command. Don't really know where to go from here, appreciate any advice . . .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
windows.h 包含winsock.h,它与winsock2.h 包含文件冲突。
在包含 windows.h: 之前定义 WINSOCKAPI 来防止第一次包含
:
windows.h includes winsock.h, which collides with the winsock2.h include file.
prevent the first inclusion by defining WINSOCKAPI before you include windows.h:
:
您应该按照 Iulian Şerbănoiu 的建议将winsock2.h 放在windows.h 之前
您也可以使用lean 和mean 宏:
msdn 上描述了原因: 创建基本 Winsock 应用程序
另外,请确保将程序链接到 WS2_32.lib 文件(这取决于什么)您正在使用的 IDE,例如 Visual Studio?)
在 Visual Studio 中,您转到“项目”>“属性>链接器>附加包含(或类似的东西,目前不在带有 Visual Studio 的计算机上。)这也在上面的链接中进行了描述。
You should place the winsock2.h before the windows.h as suggested by Iulian Şerbănoiu
You could also use the lean and mean macro:
The reason is described on msdn: Creating a Basic Winsock Application
Also make sure that you link your program to the WS2_32.lib file (this is depending on what IDE you are using, for instance Visual Studio?)
In Visual Studio you go to Project > Properties > Linker > Additional includes (or something like that, not at a computer with visual studio at the moment..) This is also described in the link above.
使用它可以避免编译错误
use this avoid compilation errors