问题描述]
   在包含了<windows.h>以及<winsock2.h>的工程中,编译有时会出现如
下错误:

     error C2011: 'fd_set' : 'struct' type redefinition
     error C2011: 'timeval' : 'struct' type redefinition
                     ....
     error C2375: 'accept' : redefinition; different linkage
[原因分析]
   主要原因是因为<windows.h>中包含了<winsock.h>头文件,由于其版
本的不同,导致出
现上述的错误。<windows.h>中相关代码如下:
               #ifndef WIN32_LEAN_AND_MEAN
               #include <cderr.h>
               #include <dde.h>
               #include <ddeml.h>
               ........
                #ifndef _MAC
               #include <winperf.h>
               #include <winsock.h>
               #endif
                .......

               #include <commdlg.h>
               #endif
               #endif
[解决方案]
    由以上代码可以看出如果在没有定义WIN32_LEAN_AND_MEAN宏
的大前
提下windows.h有可能包含winsock.h 头文件,因此我们得出一个很简单
的解决方
法就是在包含<windows.h>之前定义WIN32_LEAN_AND_MEAN宏,如
下所示:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

 

 

k1988注:还有一种解决办法就是把包含winsock2.h的语句都提到包含windows.h的前面

相关文章:

  • 2022-12-23
  • 2021-07-24
  • 2022-12-23
  • 2022-01-08
  • 2021-07-21
  • 2021-08-14
猜你喜欢
  • 2021-12-30
  • 2022-12-23
  • 2021-12-16
  • 2022-12-23
  • 2022-12-23
  • 2022-02-10
  • 2021-06-05
相关资源
相似解决方案