How do I use OS-specific APIs conditionally?

In C++, you might want to use OS-specific APIs depending on the platform your code is running on. To achieve this, you can use preprocessor directives to conditionally compile code. Below is an example demonstrating how to implement this in a C++ program using the Windows and Unix operating systems.

#include #ifdef _WIN32 #include #elif defined(__unix__) #include #endif int main() { #ifdef _WIN32 std::cout << "Running on Windows" << std::endl; Sleep(1000); // Sleep for 1000 milliseconds #elif defined(__unix__) std::cout << "Running on Unix/Linux" << std::endl; sleep(1); // Sleep for 1 second #endif return 0; }

OS-specific APIs C++ conditional compilation preprocessor directives cross-platform code Windows Unix