Winsock2.h:没有这样的文件或目录

时间:2014-10-11 18:11:02

标签: c linux compilation mingw32 windows-2000

我正在尝试使用mingw32 for Linux编译this

但是,我收到以下错误和警告:

# i586-mingw32msvc-gcc ms04-020.c -lws2_32 -o ms04-020.exe
ms04-020.c:43:22: error: Winsock2.h: No such file or directory
ms04-020.c: In function ‘main’:
ms04-020.c:113: warning: incompatible implicit declaration of built-in function ‘printf’
ms04-020.c:114:11: warning: unknown escape sequence '\;'
ms04-020.c: In function ‘readwrite’:
ms04-020.c:251: warning: incompatible implicit declaration of built-in function ‘printf’
ms04-020.c:259: warning: incompatible implicit declaration of built-in function ‘printf’
ms04-020.c:264: warning: incompatible implicit declaration of built-in function ‘printf’
ms04-020.c:279: warning: incompatible implicit declaration of built-in function ‘printf’
ms04-020.c:285: warning: incompatible implicit declaration of built-in function ‘printf’
ms04-020.c:290: warning: incompatible implicit declaration of built-in function ‘printf’
ms04-020.c:301: warning: incompatible implicit declaration of built-in function ‘printf’
ms04-020.c:307: warning: incompatible implicit declaration of built-in function ‘printf’
ms04-020.c:312: warning: incompatible implicit declaration of built-in function ‘printf’
ms04-020.c: In function ‘client_connect’:
ms04-020.c:333: warning: incompatible implicit declaration of built-in function ‘printf’
ms04-020.c:343: warning: incompatible implicit declaration of built-in function ‘printf’

如何解决有关Winsock2.h的错误?我确实设法在Windows 7上使用Visual Studio 2013进行编译,这是一个可接受的解决方案,但是compiling with Visual Studio 2010 or later makes the application incompatible with the Windows 2000 target OS。因此,即使它以这种方式构建,二进制文件也不会执行。

1 个答案:

答案 0 :(得分:5)

正如您可以猜到的那样,winsock2.h与Windows的TCP / IP套接字实现直接相关,并且只存在于Windows系统上。

Windows和Linux之间可能很难完全兼容include。恕我直言,你应该:

  • 从源代码中删除#include <winsock32.h>以进行Linux编译或更好地编写:

    #ifdef _WIN32
    #include <Winsock2.h>
    #endif
    
  • 添加(最终在#else部分中)缺少的标题

目前,warning: incompatible implicit declaration of built-in function ‘printf’表示您缺少#include <stdio.h>