Feeds

Write Portable Code

Solid principles for C and C++ developers

  • alert
  • submit to reddit

The essential guide to IT transformation

Book review Portability is one of those virtues we would all love to have in our code. One of the drivers of Java's popularity was the promise of portability across platforms, and of course that same promise exists in many of the more popular scripting languages, such as Python, Ruby and so on.

However, when it comes to coding in C or C++, portability is much, much harder to achieve, simply because coding in these languages tends to be closer to the hardware. Without a virtual machine to code to, the vagaries of real platforms makes even the best of developers hostage to the operating system and the metal it runs on. However, help and advice is on hand for C/C++ coders in the form of Write Portable Code, which might easily have been entitled How to write portable code in C or C++'.

The book opens with a discussion of what portability means and how difficult it is to take a single platform product and port it across to another platform. Unless, of course, portability has been part of the design thinking from the start. Portability, like security, is better as part of the fundamental design than as an after-thought.

Having explored the underlying concept, the book moves on to examine in more detail the technical and logistical issues that have to be faced. These range from differences in hardware, such as processors and floating point number formats, to differences in file systems, memory addressing, dynamic libraries and so on. Such differences can be a nightmare to deal with, and the book explores a variety of strategies for dealing with them.

One of the key ingredients in cross-platform code is the ability to inspect the current platform. This means much more than just querying to find out about the operating system the software is running under. Issues like big-endian vs. little-endian architectures can make even basic functions difficult. Author Brian Hook provides a partial solution in the form of POSH (Portable Open Source Header), which provides several platform introspection functions.

It's not just platform differences that can trip up the unwary. The book also looks at the logistics of coding in different platforms, from compiler differences to source control to editors, IDEs and other tools. Again, as with the other parts of the book, the author's knowledge and experience shines through, as does his writing skill.

A big plus for the book is the sample application, SAL (Simple Audio Library), which puts into practice the principles that are discussed throughout the book. Full source code for SAL and POSH are available for download, making it relatively easy to get started with code.

While the emphasis is on C and C++, other languages do get a mention. Scripting languages in particular are covered in a separate chapter that looks at JavaScript, Python and Ruby. In particular the use of an embedded interpreter is explored, showing how it can be used to code around platform differences but also shows what the trade-offs are in terms of performance and other factors.

To conclude, this book provides the reader with a mixture of solid principles, concrete examples and an extended example using a portable sound library that illustrates how the principles translate into the real world.

Write Portable Code

Author: Brian Hook

Publisher: No Starch Press

ISBN: 1593270569

Media: Book

Level: Intermediate/Advanced

Verdict: Highly practical and very useful

Buy this book at Cash & Carrion

Secure remote control for conventional and virtual desktops

More from The Register

next story
Munich considers dumping Linux for ... GULP ... Windows!
Give a penguinista a hug, the Outlook's not good for open source's poster child
The Return of BSOD: Does ANYONE trust Microsoft patches?
Sysadmins, you're either fighting fires or seen as incompetents now
Microsoft cries UNINSTALL in the wake of Blue Screens of Death™
Cache crash causes contained choloric calamity
Time to move away from Windows 7 ... whoa, whoa, who said anything about Windows 8?
Start migrating now to avoid another XPocalypse – Gartner
You'll find Yoda at the back of every IT conference
The piss always taking is he. Bastard the.
HANA has SAP cuddling up to 'smaller partners'
Wanted: algorithm wranglers, not systems giants
prev story

Whitepapers

Endpoint data privacy in the cloud is easier than you think
Innovations in encryption and storage resolve issues of data privacy and key requirements for companies to look for in a solution.
Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Top 8 considerations to enable and simplify mobility
In this whitepaper learn how to successfully add mobile capabilities simply and cost effectively.
Solving today's distributed Big Data backup challenges
Enable IT efficiency and allow a firm to access and reuse corporate information for competitive advantage, ultimately changing business outcomes.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.