In terms of things like <windows.h>
- you don't. Or at least, you try not to.
If you do end up needing to use a GUI, you'll want to look into something cross-platform (I'm fairly sure Qt has good cross-platform support, wxWidgets might as well). Either way, your goal is to write as much standard C++ code as possible and invoke as few system libraries/APIs as possible.
If you do end up needing to use system APIs, wrap them in functions, and use #ifdef
s within those functions to ensure you're calling the right API. Generally your compiler will provide a few macros that will let you determine which OS you're running on, to a certain extent, as well as 32-bit vs 64-bit.
void DoSomething()
{
#ifdef _WIN32
//do Windows things here
#endif
#ifdef __linux__
//do Linux things here
#endif
}
Or:
void DoSomethingWindows()
{
//private Windows code here
}
void DoSomethingLinux()
{
//private Linux code here
}
void DoSomething()
{
#ifdef _WIN32
DoSomethingWindows();
#endif
#ifdef __linux__
DoSomethingLinux()
#endif
}
If you really want to get fancy, you could even set up entire headers for your platform-specific code. Just use #ifdef
s to figure out your target OS and #include
accordingly.
Also, make sure you do a lot of testing on all systems involved. System APIs won't necessarily have perfect matches across different operating systems, and if you make assumptions about the way a given API acts but don't test it, you might find yourself in trouble down the road.