winsock编译错误

发布于 2024-09-06 11:07:00 字数 959 浏览 7 评论 0原文

以下错误来自仅包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放赐 2024-09-13 11:07:00

windows.h 包含winsock.h,它与winsock2.h 包含文件冲突。
在包含 windows.h: 之前定义 WINSOCKAPI 来防止第一次包含

#define _WINSOCKAPI_ 
#include "windows.h"
#include "winsock2.h"

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:

:

#define _WINSOCKAPI_ 
#include "windows.h"
#include "winsock2.h"
沉默的熊 2024-09-13 11:07:00

您应该按照 Iulian Şerbănoiu 的建议将winsock2.h 放在windows.h 之前

#include <winsock2.h>
#include <windows.h>

您也可以使用lean 和mean 宏:

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <windows.h>
#include <winsock2.h>

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

#include <winsock2.h>
#include <windows.h>

You could also use the lean and mean macro:

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <windows.h>
#include <winsock2.h>

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.

七堇年 2024-09-13 11:07:00
#pragma comment(lib, "wininet.lib")
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")

使用它可以避免编译错误

#pragma comment(lib, "wininet.lib")
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")

use this avoid compilation errors

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文