Winsock compilation error

The following errors apply to a file with windows and winsock2 enabled only.

C:\Users\ioil\Desktop\dm\bin>dmc sockit.c
typedef struct fd_set {
                      ^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(85) : Error: 'fd_set' is already defined
} fd_set;
^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(88) : Error: identifier or '( declarator )' expected
struct timeval {
               ^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(129) : Error: 'timeval' is already defined
};
^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(132) : Error: identifier or '( declarator )' expected
struct  hostent {
                ^
C:\Users\ioil\Desktop\dm\bin\..\include\win32\WINSOCK2.H(185) : Error: 'hostent' is already defined
Fatal error: too many errors
--- errorlevel 1

C:\Users\ioil\Desktop\dm\bin>

What has already been tried: placing the winsock.dll file in the same directory as the compiler and the compilation program, placing it in the system32 directory and entering it into the registry using the regsrv32 command. I don’t know where to go from here, appreciate any advice.,

+3
source share
3 answers

windows.h includes winsock.h, which encounters the include winsock2.h file. prevent the first inclusion by defining WINSOCKAPI before enabling windows.h:

:

#define _WINSOCKAPI_ 
#include "windows.h"
#include "winsock2.h"
+2
source

You should put winsock2.h in front of the windows. h as suggested by Iulian Şerbănoiu

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

:

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

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

msdn: Winsock

, WS2_32.lib( , IDE , Visual Studio?)
Visual Studio Project > Properties > Linker > Additional includes ( - , ..) .

+2
#pragma comment(lib, "wininet.lib")
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")

use this to avoid compilation errors

+1
source

Source: https://habr.com/ru/post/1750434/


All Articles