Important: Please read the Qt Code of Conduct -

What are good coding patterns to avoid "conditional compile clutter" ( #ifdef Q_OS_ANDROID ) to support multiple platforms ?

  • Problem: There will always be a problem to solve that will require use of a native API.

    Example: I have a JNI interface that sends email with attachments on Android. I will need another implementation for iOS, another for OSX and Linux, another for Windows, etc.

    What are good coding patterns to avoid "conditional compile clutter"? Can anyone point me to some code examples?

    An interface class with implementation for Android, iOS, Linux, OSX, and Windows?

    Thanks in advance for ideas and suggestions,


  • The bug report below is what got me started thinking about ways to avoid ifdef's:

    Remove Q_OS_ANDROID checks where possible

    There are a lot of #ifdef Q_OS_ANDROID around, e.g. in the style code, and that's not really the way platform integrations are done anymore. We need to see if they are necessary. In the style code it would make sense if they are inside the android style instead of spread around.

  • Qt Champions 2016

    You may implement your code by using bridge design pattern. Some Qt classes also use this design pattern.

    "Bridge pattern - Wikipedia, the free encyclopedia":

  • Lifetime Qt Champion


    Depending on the amount of code (e.g. complete methods), you can create a yourcoolclass_nameofplatform.cpp file where you'll have the implementation details that you want to separate and you'll compile the correct file for a given platform.

    Hope it helps

  • Thank you both for the suggestions!


Log in to reply