Cross platform C++ code architecture

I'm using platform neutral header files, keeping any platform specific code in the source files (using the PIMPL idiom where neccessary). Each platform neutral header has one platform specific source file per platform, with extensions such as *.win32.cpp, *.posix.cpp. The platform specific ones are only compiled on the relevent platforms.

I also use boost libraries (filesystem, threads) to reduce the amount of platform specific code I have to maintain.

It's platform independent classes declarations with platform specific definitions.

Pros: Works fairly well, doesn't rely on the preprocessor - no #ifdef MyPlatform, keeps platform specific code readily identifiable, allows compiler specific features to be used in platform specific source files, doesn't pollute the global namespace by #including platform headers.

Cons: It's difficult to use inheritance with pimpled classes, sometimes the PIMPL structs need their own headers so they can be referenced from other platform specific source files.


Another way is to have platform independent conventions, but substitute platform specific source code at compile time.

That is to say that if you imagine a component, Foo, that has to be platform specific (like sockets or GUI elements), but has these public members:

class Foo {
public:
  void write(const char* str);
  void close();
};

Every module that has to use a Foo, obviously has #include "Foo.h", but in a platform specific make file you might have -IWin32, which means that the compiler looks in .\Win32 and finds a Windows specific Foo.h which contains the class, with the same interface, but maybe Windows specific private members etc.

So there is never any file which contains Foo as written above, but only sets of platform specific files which are only used when selected by a platform specific make file.