在Winsock.h中获得未声明的标识符错误

Getting undeclared Identifier errors in Winsock.h

本文关键字:未声明 标识符 错误 Winsock      更新时间:2023-10-16

我正在尝试编译代码

#include <Header1.h>
#include "Header2..h" 
#include <ctype.h>  

Header1.h包括winsock.h, Header2.h包括windows.h

我使用winsock.h而不是winsock2.h,因为winsock2.h显示重新定义错误,这是一个标准错误,但我无法使用提供给他们的解决方案来修复。

我也试过包括ws2tcpip.h,但它在winsock.h中给出了大量的重新定义错误。

我在这个模块中得到12个错误

error C3861: 'close': identifier not found
error C2664: 'setsockopt' : cannot convert parameter 4 from 'timeval *' to 'const char *'
error C2065: 'socklen_t' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'optionLength'
error C2065: 'optionLength' : undeclared identifier
error C2065: 'optionLength' : undeclared identifier
error C2664: 'setsockopt' : cannot convert parameter 4 from 'int32 *' to 'const char *'
error C2065: 'MSG_WAITALL' : undeclared identifier
error C2664: 'recvfrom' : cannot convert parameter 2 from 'uint8 *' to 'char *'
error C2065: 'ERROR_END_OF_STREAM' : undeclared identifier
error C3861: 'close': identifier not found
error C3861: 'close': identifier not found

在include的顶部添加#include <winsock2.h>#include <ws2tcpip.h>

#include <winsock2.h> 
#include <Header1.h>
#include "Header2.h"
#include <ctype.h>  
应该首先包含

winsock.h。我注意到你的代码似乎是统一的。在windows上,closesocket用于关闭socket

另外,#在任何其他内容之前定义WIN32_LEAN_AND_MEAN。

此外,在某些情况下(ws2tcpip标识符错误的问题):

#pragma once
#define _WIN32_WINNT 0x500
#include <winsock2.h>
#include <WS2tcpip.h>
#pragma comment(lib, "WS2_32.Lib")