c++ - WinSock2.h 编译错误

标签 c++ windows-server-2003 visual-studio-2003

我正在尝试使用 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/

相关文章:

c++ - 什么是 A B::* func() 定义?

c++ - 生成没有srand的随机数

windows-server-2003 - 在 Windows Server 2003 中使用 Psexec

c - 枚举 hack 在 C 中有效吗?如果是这样,它应该在 VisualStudio 2003 中工作吗?

c++ - .dll 文件是为每个程序加载一次还是为所有程序加载一次?

c++ - 如何使用 C++ 写入文件的特定列?

Apache 和 IIS 在 Windows 2003 上并行(均监听端口 80)

asp.net - Sys 未定义 - 仅 IE 7 中的 ajax 问题

c++ - 奇怪的 C++ 语法

c# - 将 Visual Studio 2008 项目转换为 2003