C++ cross platform code

We our going to start a new project in our small team:

  • It's a library which will be used by our other projects (in Linux and Windows).
  • It's not platform dependent logically (it's not using any system calls or anything like that).
  • It has to be compiled on various platforms (including Windows and Linux at least).

    Unfortunately, non of our developers ever wrote any code in any other platform than Windows! Therefore, I have to give them a "code like this" or "not code like this" list, so the code will stay cross platform.

    any guidance?


    One way of increasing portability is to use the same compiler, GCC, on both platforms. If you also use the same compiler version, you will probably avoid most, if not all C++ language and Standard Library incompatibilities. You can also use the same build tools, such as GNU make, which means the build process is the same on both platforms.

    As for platform incompatibilities - check that their code does not contain includes like:

    #include <windows.h>
    #include <unistd.h>
    #include <sys/almost_anything.h>
    

    unless this is done via conditional compilation.


    Make sure that you have automated build processes on all platforms, and write unit and automated functional tests. If possible run automated nightly builds and tests. It's much more important when you are writing cross platform libraries.

    I would do the opposite of one of the other answers (Not that i think it's wrong, just a different approach) and if you can choose your platforms make them as different as possible. For example make one 32 bit MCVC and the other 64 bit gcc on unix. If you can have automated tests and builds then this will show up portability issues quickly.

    If possible have some developers working on one platform and some on another rather than finishing the code on one platform and then "porting" it to the other. That way they'll learn very quickly what not to do when their co workers come over to complain they broke something.

    Technically things to watch out for are

  • Don't assume ints are 32 bits
  • Don't assume char's are signed or unsigned
  • Don't assume characters are in ASCII
  • Don't assume anything about data byte order or alignment
  • Minimize pointer arithmetic. You'll only get it wrong when you make assumptions that fail.
  • Remember file and directory names work differently on different platforms. If you only have to port to windows and unix you might get awayy with it but once you've ported to two platforms then the next port might be to z-series or VMS where these things work quite differently.

  • Most importantly: have automatic builds and tests running on all supported platforms, all the time, to pick up most non-portable (or just wrong) code straight away.

    The language itself, the Standard Library, and Boost should be portable to any widely-used platform (certainly modern versions of Linux/GCC and Windows/MSVC). Be suspicious of any system header file that ends with .h , and check other libraries for portability before deciding to use them.

    Maintain a document of all the incompatibilities you encounter, so hopefully you'll only make each mistake once.

    链接地址: http://www.djcxy.com/p/54546.html

    上一篇: C ++跨平台构建自动化

    下一篇: C ++跨平台代码