我正在尝试使用 Visual Studio 2003 运行一个项目。但是我遇到了很多类似于以下内容的编译错误。
错误指向 WinSock2.h 文件。我正在从 WinSock2.h 文件和相应的错误中复制几个代码片段
typedef struct fd_set {
u_int fd_count; /* how many are SET? */
SOCKET fd_array[FD_SETSIZE]; /* an array of SOCKETs */
} fd_set;
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinSock2.h(114): error C2065: 'fd_set' : undeclared identifier
struct sockaddr {
u_short sa_family; /* address family */
char sa_data[14]; /* up to 14 bytes of direct address */
};
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinSock2.h(109): error C2143: syntax error : missing ';' before '{'
ws2_32.lib文件添加到“配置属性-链接器-输入-附加依赖项”。构建配置平台为win32。
预先感谢您的帮助。
最佳答案
可以找到具有良好头文件顺序的典型基本 Winsock 应用程序 here :
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "Ws2_32.lib")
int main() {
return 0;
}
包含头文件的顺序很重要
关于c++ - WinSock2.h 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33306922/