Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 741
batty Avatar asked Jan 10 '09 02:01

batty


People also ask

How does C++ detect operating system?

To check the operating system of the host in a C or C++ code, we need to check the macros defined by the compiler (GNU GCC or G++). For example, on Windows platform, the compiler has a special macro named _WIN32 defined. So, if the macro _WIN32 is defined, we are on Windows.

What is __ function __ in C?

(C++11) The predefined identifier __func__ is implicitly defined as a string that contains the unqualified and unadorned name of the enclosing function. __func__ is mandated by the C++ standard and is not a Microsoft extension.

How do I get the OS name in C++?

h> int uname(struct utsname *name);

What is __ FILE __ in C?

The __FILE__ macro expands to a string whose contents are the filename, surrounded by double quotation marks ( " " ). If you change the line number and filename, the compiler ignores the previous values and continues processing with the new values. The #line directive is typically used by program generators.


2 Answers

[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

like image 53
Phil Hord Avatar answered Sep 19 '22 19:09

Phil Hord


There are a number of different ways to detect compilation, host, and runtime environments. All depending on exactly what you want to know. There are three broad types of environments:

  • Build: This is the environment in which the program is compiled.
  • Host: This is the environment in which the program is being run.
  • Target: In case of code-generation tools (such as compilers), this is where the generated code will run.

If you are cross-compiling, the build and host environment can be completely different (this is common when building embedded applications, but not very common when building desktop/server apps), and you typically cannot run the compiled binary on the system used to compile it. Otherwise, the host environment must be compatible with the build environment: for example, building an application on XP which will run on Vista.

C preprocessor macros can not be used to tell you the details of the host system (i.e. what you are running on); they can only tell you what the code was compiled for. In the windows case, the two most important macros are:

  • _WIN32 signifies that the Win32 API is available. It does not tell you which compiler you are using, in fact _WIN32 is defined both when using Cygwin's GCC and MinGW's GCC. So, do not use _WIN32 to figure out if you're being compiled with Visual Studio.
  • _MSC_VER tells you that you the the program is being compiled with Microsoft Visual C/C++. Well, almost. _MSC_VER is also defined when using Intel's C++ compiler which is intended to be a drop-in replacement for Visual C++.

There are a bunch of other macros described in the Visual Studio documentation.

If you want to know which exact version of Windows you are using, you'll have to use runtime functions such as GetVersion() (as described in other answers).

You might get more specific answers if you told us exactly what you want to check for.

like image 29
JesperE Avatar answered Sep 17 '22 19:09

JesperE