Feeds

Write Portable Code

Solid principles for C and C++ developers

  • alert
  • submit to reddit

Secure remote control for conventional and virtual desktops

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

The essential guide to IT transformation

More from The Register

next story
The Return of BSOD: Does ANYONE trust Microsoft patches?
Sysadmins, you're either fighting fires or seen as incompetents now
Munich considers dumping Linux for ... GULP ... Windows!
Give a penguinista a hug, the Outlook's not good for open source's poster child
Intel's Raspberry Pi rival Galileo can now run Windows
Behold the Internet of Things. Wintel Things
Linux Foundation says many Linux admins and engineers are certifiable
Floats exam program to help IT employers lock up talent
Microsoft cries UNINSTALL in the wake of Blue Screens of Death™
Cache crash causes contained choloric calamity
Eat up Martha! Microsoft slings handwriting recog into OneNote on Android
Freehand input on non-Windows kit for the first time
Linux kernel devs made to finger their dongles before contributing code
Two-factor auth enabled for Kernel.org repositories
prev story

Whitepapers

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 10 endpoint backup mistakes
Avoid the ten endpoint backup mistakes to ensure that your critical corporate data is protected and end user productivity is improved.
Top 8 considerations to enable and simplify mobility
In this whitepaper learn how to successfully add mobile capabilities simply and cost effectively.
Rethinking backup and recovery in the modern data center
Combining intelligence, operational analytics, and automation to enable efficient, data-driven IT organizations using the HP ABR approach.
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.