Skip to content

check OS with a preprocessor directive

To check the operating system using a preprocessor directive, you can use the #ifdef and #ifndef directives, along with the appropriate macro for the operating system you want to check for. For example, to check if the operating system is Linux, you can use the following code:

cpp
#ifdef __linux__
// code for Linux operating system
#endif

Similarly, to check if the operating system is Windows, you can use the following code:

cpp
#ifdef _WIN32
// code for Windows operating system
#endif

These preprocessor directives check if the specified macro is defined, and if it is, the code inside the #ifdef block will be compiled. Otherwise, it will be ignored. You can use this technique to check for any operating system and include the appropriate code for that operating system in your program.

To check if the operating system is macOS, you can use the following code:

cpp
#ifdef __APPLE__
// code for macOS operating system
#endif

This code uses the __APPLE__ macro, which is defined by the compiler when compiling for macOS. You can use this code to include the appropriate code for macOS in your program.

Note that the __APPLE__ macro will also be defined when compiling for iOS, so if you want to check specifically for macOS and not iOS, you can use the following code instead:

cpp
#if defined(__APPLE__) && defined(__MACH__)
// code for macOS operating system
#endif

This code checks if both the __APPLE__ and __MACH__ macros are defined, which indicates that the program is being compiled for macOS.

吃好喝好 快乐地活下去