The easy way is to write it at a higher level, and let the abstraction handle the meat of it for you. The easiest way is just to use an engine, an existing array of tools and frameworks that manages the abstraction in such a manner that (for the most part) you just have to select the build target.
In this specific case, I used the Unity engine. I write in C#, and with Mono/OpenGL, Unity can build executables to various platforms, on (mostly) the same code.
This won't work for all apps, but (for example) a lot of simple mobile apps use Cordova with faux-native UI frameworks, and there are a lot of graphics-lite games out there using NW.js or Electron on desktop.