Are there any macros to determine if my code is being compiled to Windows? [duplicate]

[Edit: I assume you want to use compile-time macros to determine which environment you’re on. Maybe you want to determine if you’re running on Wine under Linux or something instead of Windows, but in general, your compiler targets a specific environment, and that is either Windows (DOS) or it isn’t, but it’s rarely (never?) both.]

Some compilers offer macros to indicate a Windows build environment. But these will vary from compiler to compiler, and even on the same compiler on Windows if the target environment is not exclusively windows. Usually it’s __WIN32__, but not always.

#if defined (__WIN32__)
  // Windows stuff
#endif

Sometimes it can be _WIN32, __CYGWIN32__, or possibly just the compiler indicator (_MSC_VER).

If you know the environment you’ll be building in (from the makefile) then you can usually pass in the #define on the command line, like “g++ -D __WIN32__ yourfile.c“.

A little more info here

Leave a Comment