Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-platform library design. Best practices [closed]

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?

like image 335
HighPredator Avatar asked Oct 30 '25 03:10

HighPredator


1 Answers

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().

like image 116
dpi Avatar answered Oct 31 '25 18:10

dpi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!