Winsock2.h: No such file or directory

SilverlightFox picture SilverlightFox · Oct 11, 2014 · Viewed 22.1k times · Source

I'm trying to compile this with mingw32 for Linux.

However, I get the following errors and warnings:

# 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’

How can I fix the error regarding Winsock2.h? I did manage to get this to compile using Visual Studio 2013 on Windows 7 which would be an acceptable solution, however compiling with Visual Studio 2010 or later makes the application incompatible with the Windows 2000 target OS. So even though it builds in this manner, the binary won't execute.

Answer

Serge Ballesta picture Serge Ballesta · Oct 11, 2014

As you could guess with its name, winsock2.h is directly related to Windows implementation of TCP/IP sockets, and exists only on Windows systems.

It may be hard to have full compatibility of include between Windows and Linux. IMHO, you should :

  • remove #include <winsock32.h> from your source for Linux compilation or better write :

    #ifdef _WIN32
    #include <Winsock2.h>
    #endif
    
  • add (eventually in a #else section) the missing headers

Currently, warning: incompatible implicit declaration of built-in function ‘printf’ shows that you are lacking a #include <stdio.h>