Cross platform C++ code architecture

前端 未结 8 618
广开言路
广开言路 2021-01-31 00:18

I\'m having a bit of a go at developing a platform abstraction library for an application I\'m writing, and struggling to come up with a neat way of separating my platform indep

8条回答
  •  故里飘歌
    2021-01-31 00:58

    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.

提交回复
热议问题