17

I am using mingw32-make to compile a project to Windows, which depends on a project called libevent2. I keep receiving this error -

util.h:69:25: fatal error: sys/socket.h: No such file or directory 

Obviously a file from the Linux API is trying to be included, which won't work on Windows. Upon further investigation I find however that this file should only be included if WIN32 isn't defined.

#ifdef WIN32 #include <winsock2.h> #else #include <sys/socket.h> #endif 
3
  • "Your project"? Ensure WIN32 is defined. '<sys/socket.h>` requires cygwin, not just mingw32. Commented Jan 26, 2012 at 21:39
  • @John Sorry, 'a project'. What do you mean 'ensure WIN32 is defined?'. Commented Jan 26, 2012 at 21:44
  • What version of MinGW are you using? What's the command line invoking the compiler? Commented Jan 26, 2012 at 23:10

3 Answers 3

20

You should use _WIN32 and may also want to check for __CYGWIN__

#if defined _WIN32 || defined __CYGWIN__ 
Sign up to request clarification or add additional context in comments.

Comments

13

Are you sure there's nothing undefining WIN32? My installation of MinGW (4.6.1 at this site) definitely defines it:

C:\temp>gcc -E -dM test.c | find /i "win" #define _WIN32 1 #define __WINT_MAX__ 65535 #define _WINT_T #define __WINT_MIN__ 0 #define __WIN32 1 #define __WINNT 1 #define __WINNT__ 1 #define __WIN32__ 1 #define __SIZEOF_WINT_T__ 2 #define WIN32 1 // <-- right here #define __WINT_TYPE__ short unsigned int #define WINNT 1 

Try passing the -E -dM options to verify if your MinGW compiler is (or isn't) pre-defining the WIN32 macro.

Note that strictly speaking, WIN32 should not be predefined by the compiler (since it's in the user's namespace) - only _WIN32 should. WIN32 should be set by the SDK being used and/or by the build environment - that's the way it works in Microsoft's compilers.

For example, there's the following sequence in windef.h"

#ifndef WIN32 #define WIN32 #endif 

and /D "WIN32" is put into Visual Studio C++ projects by default.

See https://stackoverflow.com/a/662543/12711 for more details.

Comments

1

GCC does not define WIN32 on Windows when it compiles based on a C++ standard, such as -std=c++11 or other year. However, _WIN32 is defined. When GNU extensions are enabled, which is normally the default or when specific GNU extensions are enabled with -std=gnu++??, then WIN32 is defined together with _WIN32.

Use _WIN32 instead of WIN32 when possible. When it is impractical to replace it, either use the default GNU extensions without any switch or specific year with -std=gnu++?? or define this macro in your build system. E.g. in the Makefile:

> ifeq ($(OS),Windows_NT) > CXXFLAGS += -DWIN32 > endif 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.