I would like to detect whether the OS I’m compiling on is Windows. Is there a simple macro I can check to verify that?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
[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.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
#defineon the command line, like ‘g++ -D __WIN32__ yourfile.c‘.A little more info here