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

Top 5 reasons to deploy VMware with Tegile

More from The Register

next story
New 'Cosmos' browser surfs the net by TXT alone
No data plan? No WiFi? No worries ... except sluggish download speed
iOS 8 release: WebGL now runs everywhere. Hurrah for 3D graphics!
HTML 5's pretty neat ... when your browser supports it
Mathematica hits the Web
Wolfram embraces the cloud, promies private cloud cut of its number-cruncher
Mozilla shutters Labs, tells nobody it's been dead for five months
Staffer's blog reveals all as projects languish on GitHub
'People have forgotten just how late the first iPhone arrived ...'
Plus: 'Google's IDEALISM is an injudicious justification for inappropriate biz practices'
SUSE Linux owner Attachmate gobbled by Micro Focus for $2.3bn
Merger will lead to mainframe and COBOL powerhouse
iOS 8 Healthkit gets a bug SO Apple KILLS it. That's real healthcare!
Not fit for purpose on day of launch, says Cupertino
prev story

Whitepapers

Secure remote control for conventional and virtual desktops
Balancing user privacy and privileged access, in accordance with compliance frameworks and legislation. Evaluating any potential remote control choice.
Intelligent flash storage arrays
Tegile Intelligent Storage Arrays with IntelliFlash helps IT boost storage utilization and effciency while delivering unmatched storage savings and performance.
WIN a very cool portable ZX Spectrum
Win a one-off portable Spectrum built by legendary hardware hacker Ben Heck
High Performance for All
While HPC is not new, it has traditionally been seen as a specialist area – is it now geared up to meet more mainstream requirements?
Beginner's guide to SSL certificates
De-mystify the technology involved and give you the information you need to make the best decision when considering your online security options.