[Mingw-users] Including winsock2.h considered harmful?

Back to archive index

Eli Zaretskii eliz****@gnu*****
Sat Jul 28 22:12:04 JST 2018


I've upgraded to MinGW runtime 5.0.2, and several programs now fail to
compile because they include winsock2.h and then expect error codes
from winerror.h to be defined.  Here's a typical example, from Gawk:

  gcc -c -D__USE_MINGW_ANSI_STDIO -DHAVE_LIBREADLINE -DHAVE_MPFR -O2 -gdwarf-2 -g3 -Wno-deprecated-declarations -DGAWK -I. -I./support -DHAVE_CONFIG_H -DDEFLIBPATH="\"d:/usr/lib/gawk\"" -DSHLIBEXT="\"dll\"" -DLOCALEDIR="\"d:/usr/share/locale\"" gawkmisc.c
  In file included from gawkmisc.c:36:0:
  pc/gawkmisc.pc: In function 'dlopen':
  pc/gawkmisc.pc:756:18: error: 'ERROR_INVALID_PARAMETER' undeclared (first use in this function); did you mean 'WSA_INVALID_PARAMETER'?
	 last_err = ERROR_INVALID_PARAMETER;
		    ^~~~~~~~~~~~~~~~~~~~~~~
		    WSA_INVALID_PARAMETER
  pc/gawkmisc.pc:756:18: note: each undeclared identifier is reported only once for each function it appears in
  pc/gawkmisc.pc: In function 'dlsym':
  pc/gawkmisc.pc:814:18: error: 'ERROR_INVALID_PARAMETER' undeclared (first use in this function); did you mean 'WSA_INVALID_PARAMETER'?
	 last_err = ERROR_INVALID_PARAMETER;
		    ^~~~~~~~~~~~~~~~~~~~~~~
		    WSA_INVALID_PARAMETER
  pc/gawkmisc.pc: In function 'w32_maybe_set_errno':
  pc/gawkmisc.pc:1013:12: error: 'ERROR_BAD_PIPE' undeclared (first use in this function); did you mean 'LZERROR_BADVALUE'?
	 case ERROR_BAD_PIPE:
	      ^~~~~~~~~~~~~~
	      LZERROR_BADVALUE
  pc/gawkmisc.pc:1014:12: error: 'ERROR_PIPE_BUSY' undeclared (first use in this function)
	 case ERROR_PIPE_BUSY:
	      ^~~~~~~~~~~~~~~
  pc/gawkmisc.pc:1015:12: error: 'ERROR_NO_DATA' undeclared (first use in this function); did you mean 'IMM_ERROR_NODATA'?
	 case ERROR_NO_DATA:
	      ^~~~~~~~~~~~~
	      IMM_ERROR_NODATA
  pc/gawkmisc.pc:1016:12: error: 'ERROR_PIPE_NOT_CONNECTED' undeclared (first use in this function); did you mean 'WN_NOT_CONNECTED'?
	 case ERROR_PIPE_NOT_CONNECTED:
	      ^~~~~~~~~~~~~~~~~~~~~~~~
	      WN_NOT_CONNECTED

This happens because some header included by the source file does
this:

  #if !defined _WIN32_WINNT || _WIN32_WINNT < 0x501
  # undef _WIN32_WINNT
  # define _WIN32_WINNT 0x501
  #endif
  #include <winsock2.h>
  #include <ws2tcpip.h>

This used to work with MinGW runtime 3.x, but now fails, evidently
because winsock2.h no longer includes windows.h, and this doesn't
define the ERROR_* macros.  Instead, it does this:

  #define WSA_IO_PENDING                  (ERROR_IO_PENDING)
  #define WSA_IO_INCOMPLETE               (ERROR_IO_INCOMPLETE)
  #define WSA_INVALID_HANDLE              (ERROR_INVALID_HANDLE)

etc, i.e. it redirects winsock errors to "common" errors.  But it
doesn't seem to take care of defining those common errors.  So I need
to include winerror.h before winsock2.h, in order to have those error
codes defined.

Do these programs do something that isn't "kosher" nowadays?  What
should a program do if it wants to include winsock2.h, but doesn't
want to include windows.h before that?  Is that allowed?

TIA




More information about the MinGW-Users mailing list
Back to archive index