How to detect reliably Mac OS X, iOS, Linux, Windows in C preprocessor?
There are predefined macros that are used by most compilers, you can find the list here. GCC compiler predefined macros can be found here.
Here is an example for gcc:
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
//define something for Windows (32-bit and 64-bit, this part is common)
//define something for Windows (64-bit only)
//define something for Windows (32-bit only)
// iOS, tvOS, or watchOS Simulator
// Mac's Catalyst (ports iOS API into Mac, like UIKit).
// iOS, tvOS, or watchOS device
// Other kinds of Apple platforms
# error "Unknown Apple platform"
// Below __linux__ check should be enough to handle Android,
// but something may be unique to Android.
#elif __unix__ // all unices not caught above
# error "Unknown compiler"
The defined macros depend on the compiler that you are going to use.
#ifdef can be nested into the
_WIN32 is even defined when targeting the Windows x64 version. This prevents code duplication if some header includes are common to both
WIN32 without underscore allows IDE to highlight the right partition of code).
How do I check OS with a preprocessor directive?
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
Unix (Linux, *BSD, but not Mac OS X)
See this related question on some of the pitfalls of using this check.
Mac OS X
__APPLE__ Also used for classic
Both are defined; checking for either should work.
linux Obsolete (not POSIX compliant)
__linux Obsolete (not POSIX compliant)
C Preprocessor Mac OS
I think the sourceforge documentation means that you can use either
Detect Windows or Linux in C, C++
It's generally done like this (more or less):
#define DIV 1048576
#define WIDTH 7
int main(int argc, char *argv)
statex.dwLength = sizeof (statex);
_tprintf (TEXT("There is %*ld %% of memory in use.\n"),
int flag = 0;
int TotalMem, TotalFree, TotalUsed;
sprintf(cmd,"free -t -m|grep Total");
fp = popen(cmd, "r");
while ( fgets( line, sizeof line, fp))
sscanf(line,"%*s %d %d %d",&TotalMem, &TotalUsed, &TotalFree);
printf("TotalMem:%d -- TotalUsed:%d -- TotalFree:%d\n",TotalMem,TotalUsed,TotalFree);
This way, only code for linux will be compiled while on a linux platform, and only windows code will be compiled on a windows platform.
How to check if the current operating system is Windows, Linux, or OSX?
The use of
#ifdef uses preprocessor symbols to conditionally compile code depending on which symbols are defined. In your example, the compiler or an
#include file may define
WIN32, which means the code is being compiled in a Win32 environment.
Depending on the compiler and the platform, there may be different predefined symbols that indicate the operating system or processor architecture (among many other possible things) that relate to the current compilation environment.
gcc, you can show the predefined symbols using the following command line:
gcc -E -dM - </dev/null
On my machine, one of the defined symbols is:
#define __FreeBSD__ 8
This happens to mean that I'm running FreeBSD version 8.
(The above applies to C and C-family languages such as C++.)
C++ compiling on Windows and Linux: ifdef switch
//linux code goes here
// windows code goes here