【问题标题】:Getting undeclared Identifier errors in Winsock.h在 Winsock.h 中获取未声明的标识符错误
【发布时间】:2013-06-15 14:13:18
【问题描述】:

我正在尝试编译代码

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

Header1.h 包括winsock.hHeader2.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

【问题讨论】:

  • 你能分享给你错误的代码吗?

标签: c++ visual-studio-2008 networking winsock winsock2


【解决方案1】:

在包含的顶部添加#include &lt;winsock2.h&gt;#include &lt;ws2tcpip.h&gt;

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

应该首先包含winsock.h。而且我注意到您的代码似乎是 unixish 代码。在 windows 上,closesocket 用于关闭套接字。

【讨论】:

  • 感谢 mattn 我添加了#include 并删除了一些错误
【解决方案2】:

另外,#define WIN32_LEAN_AND_MEAN 先于其他。这通常会有所帮助。

【讨论】:

    【解决方案3】:

    另外,在某些情况下也有帮助(ws2tcpip 标识符错误的问题):

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

    【讨论】:

      猜你喜欢
      • 2022-07-08
      • 2013-03-12
      • 2013-01-22
      • 2011-10-25
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-16
      相关资源
      最近更新 更多