What are the best practices to design functions that have different platform-specific implementations?
For example, I have a function that structurally looks like this in a library module (it is exported):
void foo()
{
#ifdef PLATFORM_WINDOWS
// windows-specific implementation
#elif PLATFORM_LINUX
// linux-specific implementation
#elif PLATFORM_SOLARIS
// solaris-specific implementation
#endif
}
Each section can (and does atm) contain a lot of code, which makes it difficult to read and so on.
What is the proper way to do things like this?
If the implementations are completely different, it may be a good idea to not use preprocessor conditions, but have separate .c files for each platform. Each would contain different, platform-specific implementations of the same functions declared in a shared header file. The correct file would then be selected by the build system.
For example in GLFW, there is x11_window.c and win32_window.c, and both implement the same functions, like _glfwPlatformGetWindowSize().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With