I need my code to do different things based on the operating system on which it gets compiled. I\'m looking for something like this:
#ifdef OSisWindows
// do
On MinGW, the _WIN32
define check isn't working. Here's a solution:
#if defined(_WIN32) || defined(__CYGWIN__)
// Windows (x86 or x64)
// ...
#elif defined(__linux__)
// Linux
// ...
#elif defined(__APPLE__) && defined(__MACH__)
// Mac OS
// ...
#elif defined(unix) || defined(__unix__) || defined(__unix)
// Unix like OS
// ...
#else
#error Unknown environment!
#endif
For more information please look: https://sourceforge.net/p/predef/wiki/OperatingSystems/
Use #define OSsymbol
and #ifdef OSsymbol
where OSsymbol is a #define
'able symbol identifying your target OS.
Typically you would include a central header file defining the selected OS symbol and use OS-specific include and library directories to compile and build.
You did not specify your development environment, but I'm pretty sure your compiler provides global defines for common platforms and OSes.
See also http://en.wikibooks.org/wiki/C_Programming/Preprocessor
I think you are looking for:
_WIN32
- Defined as 1 when the compilation target is 32-bit ARM, 64-bit ARM, x86, or x64. Otherwise, undefined_WIN64
- Defined as 1 when the compilation target is 64-bit ARM or x64. Otherwise, undefined.
I think you are looking for:
__GNUC__
__GNUC_MINOR__
__GNUC_PATCHLEVEL__
Do a google for your appropriate compilers pre-defined.
Sorry for the external reference, but I think it is suited to your question:
C/C++ tip: How to detect the operating system type using compiler predefined macros
The Predefined Macros for OS site has a very complete list of checks. Here are a few of them, with links to where they're found:
_WIN32
Both 32 bit and 64 bit
_WIN64
64 bit only
See this related question on some of the pitfalls of using this check.
unix
__unix
__unix__
__APPLE__
__MACH__
Both are defined; checking for either should work.
__linux__
linux
Obsolete (not POSIX compliant)
__linux
Obsolete (not POSIX compliant)
__FreeBSD__
__ANDROID__
show GCC defines on Windows:
gcc -dM -E - <NUL:
on Linux:
gcc -dM -E - </dev/null
Predefined macros in MinGW:
WIN32 _WIN32 __WIN32 __WIN32__ __MINGW32__ WINNT __WINNT __WINNT__ _X86_ i386 __i386
on UNIXes:
unix __unix__ __unix