Differences between revisions 10 and 11
Revision 10 as of 2008-02-13 12:35:29
Size: 2826
Editor: mpiat1403
Comment:
Revision 11 as of 2008-02-13 22:21:37
Size: 2852
Editor: T0b05
Comment: -pedantic-errors
Deletions are marked like this. Additions are marked like this.
Line 7: Line 7:
 * Use the {{{-ansi}}} or a comparable compiler option.  * Use the {{{-ansi}}} and {{{-pedantic-errors}}} or comparable compiler options.

Tips for writing portable code

Many useful tips are taken from the book [http://proquest.safaribooksonline.com/9780321246424 Cross-Platform Development in C++: Building Mac OS X, Linux, and Windows Applications] by Syd Logan. This book describe the essential best practices of a cross-platform development culture that was adopted companywide within Netscape, and embraced by Mozilla, which was critical in enabling Netscape and Mozilla to ship product with approximately the same level of quality across a wide spectrum of platforms to tens of millions of users.

Coding

  • Use the -ansi and -pedantic-errors or comparable compiler options.

  • Use typedefs for integer types whenever the integer must be of a definite size. For example, define and use the type INT32 for integers that must consist of exactly 32 bits on all target platforms.

  • C and C++ do not specify whether the char data type is signed or unsigned. This can be a problem when mixing char and int types in code — the classic example is reading characters from stdin in C into a unsigned char using the getchar() function, which returns an int and typically uses –1 to indicate end of file. If you mix char and int types, explicitly declare the char as signed or unsigned.
  • Use the C and C++ Standard Library (part of which is the STL) whenever possible.

Policy and Management

  • Make all of your platforms a priority from the very beginning on: To do cross-platform software well, parity, both in terms of functionality and quality, must be fully met on all the supported platforms, at all phases of the product's development and release. Perhaps the scariest situation one can face in cross-platform development is this question: "Now that we have it working on platform x, what about getting it to work on platform y?"
  • Code from a common codebase: When code cannot be shared, it should be hidden behind abstractions that provide a unified application programming interface (API). Applications using the abstract API should do so ignorant that the API is abstracting platform-specific functionality. The [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract Factory Pattern] may be very useful here.

  • Organize the project in your repository. Example layout:

lib
   lib1
         mac
                include
                debug
                release
         win
                include
                debug
                release
         linux
                include
                debug
                release
   lib2
         [...]
src
   component1
          src
               mac
               win
               linux
          inc
               mac
               win
               linux
   component2
          [...]
   main

CompleteSearch: completesearch/WritingPortableCode (last edited 2008-10-23 11:40:12 by mpiat1403)