Catch up on stories from the past week (and beyond) at the Slashdot story archive

 



Forgot your password?
typodupeerror
×
Programming Books Media Book Reviews IT Technology

Write Portable Code 397

Simon P. Chappell writes "Much as a certain large software company located in the North-West of the United States of America might wish otherwise, there are many different operating systems and platforms in use in the world today. Software these days needs to able to operate in a disparate environment, either by communicating with these other platforms or by running on them or, increasingly, doing both. The Information Systems industry is making good progress with the communication half of the problem (even if a lot of it seems to involve large amounts of XML), but it is still struggling with the issues inherent with writing portable code. Brian Hook's contribution to all of this is Write Portable Code , which according to it's subtitle is an introduction to developing software for multiple platforms." Read on for the rest of Simon's review.
Write Portable Code
author Brian Hook
pages 248 (14 page index)
publisher No Starch Press
rating 8/10
reviewer Simon P. Chappell
ISBN 1593270569
summary I recommend this book to anyone working with portable code.


This is a book for computer programmers who write software designed to run on multiple platforms. It's also for programmers who suspect that their software may need to run on different platforms. This brings the book onto the radar for free and open source software authors, as they seek to create software that does not trap their end users into using specific operating systems. The Structure

There is a good progression shown in the eighteen chapters of the book. The first couple of chapters introduce the reader to portability concepts and then to some of the specific portability features of ANSI C and C++ that are used throughout the rest of the book.

The middle chapters of the book, cover individual portability topics. Some of these topics are the obvious ones, like Floating Point numbers, Networking, Operating System, File System and Dynamic Libraries. Other topics are less intuitively associated with portability, but when you read the chapter, it's inclusion is both obvious and necessary. These subjects include Source Code Management, Compilers, Scalability and Data. There is more to portability than many of us might suspect.

The last two chapters look at some alternative ways of getting portability. Scripting languages are discussed and the pros and cons of each ones portability is weighed. Lastly the use of cross-platform libraries and toolkits is addressed. Quite apropos given that the book's author is also the author of a cross platform library.

As an example of the thoughtful approach taken in this book, lets' take a look at the chapter on scripting languages. It's about the shortest chapter in the book, but representative of the approach that Mr. Hook brings to his work. This chapter takes a very honest look at the portability and cross-platform aspects of using scripting languages. There are advantages and disadvantages to the use of scripting languages. The advantages include everything that is a disadvantage of low-level languages like C/C++. Scripting languages do not require you to worry about about memory allocation, bindings, System API calls or any of the other bugbears of a low-level language programmer's life. The disadvantages of scripting languages naturally include performance, given their interpreted natures, a general lack of tools, such as development environments or IDEs and their tendency to sit high above the operating system with a corresponding detachment from low-level facilities and services of that same operating system. Mr. Hook's choice of scripting languages to consider was interesting. I expected Ruby and Python; both popular and capable in their own right. The inclusion of JavaScript/ECMAScript was also not too unexpected, now that standalone versions are bubbling up and becoming available. The real surprise, albeit a pleasant one, was the inclusion of Lua; a scripting language designed for platform portability and which seems to have managed to fully mature without making a blip on most geeks radar screens.

I like that Mr. Hook has experience writing portable software. This matched with his authorship of the Portable Open Source Harness (POSH) portability library and his contributions to the Simple Audio Library (SAL) gives a great deal of credence to his writing.

This is a solid "doing" book. Mr. Hook is under no illusion that he's writing an introduction to programming. This book has a consistent purpose to take experienced programmers and fully equip them to deal with portability and it does not deviate from this in the slightest.

The layout of the book is first rate, with clear typography, comfortable spacing, clear diagrams and tables and nicely highlighted callouts. I did not notice any obvious typos or glitches in the book. While the look of a book is not the author's fault if it is below par, a well presented book can enhance the reading and learning experience.

The examples are as realistic as possible. While some of the examples to teach principles might be simpler, they are typically backed up with examples from either the POSH or SAL projects, showing real world portability coding. The level of C/C++ required to understand the examples is higher than many books that I've read. That's not to say that the code seems obfuscated, but it's code that is taking into account aspects of the real world and is, by necessity, not simple. A further positive quality of the code examples is that they're very well explained; well enough that an inexperienced programmer with determination could follow them and come to an understanding.

Appendix B contains a summary of all of the portability rules presented through the book. There are twenty rules and each is reprised with a small explanation/reminder of it's application. An example: Rule 4 - "Never read or write structures monolithically from or to memory. Always read and write structures one element at a time, so that endian, alignment, and size differences are factored out."

If you're looking for more of a fluffy "about" book, then this is not it. This is not a complaint, rather I offer it as something to consider, before you buy what you might otherwise think is a beginner's book.

I must reiterate the non-trivial C/C++ example code the book contains. This book is for serious programmers and is not afraid to role up it's sleeves and cut real code.

This is a very well written and very readable book. There are many aspects to the subject matter of portability and Mr. Hook addresses more of them than many of us had previously suspected existed and addresses them with firm authority. I recommend this book to anyone working with portable code."


You can purchase Write Portable Code from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
This discussion has been archived. No new comments can be posted.

Write Portable Code

Comments Filter:
  • web apps (Score:3, Insightful)

    by xikzantric ( 929298 ) on Wednesday November 09, 2005 @02:43PM (#13990691)
    and this is why web applications are becoming so popular...it's much easier to make something written in PHP and distributed through the web available to everyone than to try to port something in C++ across a bunch of platforms. imho this trend towards AJAX and more web applications is a good thing and makes it easier on developers trying to deal with clients on multiple platforms. i don't want to have to deal with porting applications (although cross-browser compatibilities offer their own complications).
  • Re:web apps (Score:3, Insightful)

    by TedCheshireAcad ( 311748 ) <ted AT fc DOT rit DOT edu> on Wednesday November 09, 2005 @02:48PM (#13990735) Homepage
    AJAX and fat web apps are turning the browser into the new development "platform". The browser isn't nearly as robust a platform as, say, Java, only by virtue of the statelessness of HTTP. Not everything needs to be a web app. I know eventually some jackass is going to make an AJAX word processor/spreadsheet, but does anyone else see that this is just wrong?
  • Portable Mac apps? (Score:4, Insightful)

    by I'm Don Giovanni ( 598558 ) on Wednesday November 09, 2005 @02:49PM (#13990740)
    How can I write portable versions of Mac OS X apps when the Cocoa API doesn't exist outside of Mac OS X (don't tell me about YellowBox or what-have-you) and the language Objective C isn't supported outside of Mac OS X (Apple is killing off Cocoa's Java support)? Oh, and the Carbon API doesn't exist outside of Mac OS X either (but at least it uses a widely supported language). You mentioned a software company in the Northwest US, but what about the one in Cupertino? Apps written to their platform are no more portable than Windows apps.

    Besides that, apps that aren't able to take advantave of the underlying platform's unique features aren't sellable. Mac users in particular want apps that take advantage of the unique features of Mac OS X (and no, they don't consider some unix app to be a real "Mac" app, and rightly so). That means Cocoa or Carbon, and neither api is supported outside of Mac OS X.
  • by everphilski ( 877346 ) on Wednesday November 09, 2005 @02:50PM (#13990753) Journal
    I write simulations with GUI's in C++ exclusively and I develop in both Linux and XP (depends on the customer as to the final platform, but I code on both depening on where I am). Its easy to do c++ cross-platform. The only difference is I type "make" on the Linux box and "nmake" on the Windows box :P

    hint: use standard C++ calls, don't get locked in to vendor-specific functions, use cross-platform libraries for the rest (opengl, xerces, etc.)

    -everphilski-
  • Trolling? (Score:5, Insightful)

    by CDPatten ( 907182 ) on Wednesday November 09, 2005 @02:50PM (#13990757) Homepage
    "as a certain large software company located in the North-West of the United States of America might wish otherwise, there are many different operating systems and platforms in use in the world today. "

    If his first sentence isn't trolling I don't know what is. Why is it ok to do it against MS, but nobody else?
    You do it against MS, you make the front page, against apple you get flamed.
  • by dslauson ( 914147 ) on Wednesday November 09, 2005 @02:52PM (#13990772) Journal
    I got my first job and started digging into some code that was written for portability, it all seemed so obfuscated.

    I was like, "Why are they #defining all their data types to something else? And what's with all the crazy compiler directives?" It seemed like they were going out of their way to make the code less readable.

    Once I figured out that it was all there so that the code could be recompiled for different platforms, it all clicked together. It's really cool, and I'm pissed that I got out of college not knowing this stuff. It should be a required course, IMHO.
  • by drgroove ( 631550 ) on Wednesday November 09, 2005 @02:53PM (#13990778)
    are done w/ J2SE using SWT as the front end. Looks like a native app, runs super fast since it relies on native widgets, and portability issues are largely mitigated for me.
  • by Dr. Manhattan ( 29720 ) <(moc.liamg) (ta) (171rorecros)> on Wednesday November 09, 2005 @02:57PM (#13990810) Homepage
    How can I write portable versions of Mac OS X apps when the Cocoa API doesn't exist outside of Mac OS X

    Well, you factor the UI away from the engine. The guts can do things portably, perhaps with a few wrappers (I have my own personal set that abstracts threads, so I can use POSIX threads and Windows threads without having to change the guts around). The user interface can be as Maclike as you want.

  • Re:Small book (Score:3, Insightful)

    by Daniel_Staal ( 609844 ) <DStaal@usa.net> on Wednesday November 09, 2005 @02:58PM (#13990826)
    Java doesn't run everywhere: For instance, there is no Java JVM for a Palm. C/C++ programs that started elsewhere have been ported to Palm, on occasion.

    It's a bit of an extreme case, I admit, but...
  • blah (Score:1, Insightful)

    by Anonymous Coward on Wednesday November 09, 2005 @03:00PM (#13990843)
    let me guess. There must be a chapter that describes the operation of autoconf and automake and tell you how to check that HP-UX98/version 2.3.45 has a broken strfchok() function that doesn't accept a second argument of 0.

    These systems must die. POSIX should be the only supported target for young programmers. HP-UX87 and SunOS 0.x can RIP. Their admins should upgrade to a newer OS if they want to run new programs.

    Help deprecate those beasts! Don't write portable code for ancient systems.
  • by Anonymous Coward on Wednesday November 09, 2005 @03:01PM (#13990850)
    First, C. was going to free us from non-portable code, by abstracting away the underlying assembly language. But hardware was different, and required different libraries to communicate with it. Not all the libraries were identical, and things diverged.


    Then device drivers and operating systems were going to let us abstract away the details of the underlying implementation of the hardware, letting us write portable code. But not all O/S versions are compatible; there are glibc issues under Linux, and so on.


    Then JAVA was going to be completely portable to all operating systems. But not all Java virtual machines are identical, and different version of Java came out, and things diverged.


    All of these things made life more portable, to some degree. All of them still require boostrapping a system that understands the underlying hardware and deals with it efficiently; then abstracts all that hardware specific efficiency away again.


    Portability is *hard*: in some sense, it's the enemy of efficiency. You need to abstract away all those nice hardware specifics that make the hardware work, and replace them with a theoretical construct that caters to the lowest common denominator that you're willing to support.


    What's worse, as soon as someone makes a different design decision, or an improvement, or something that isn't universally adopted all at once, you have multiple versions -- divergent standards which aren't completely compatible. It happened with UNIX; it's happening with Linux (to a lesser extent, because code can merge again after a fork), and it happened with C and Java.


    What can the developer do? Just our best; true universal portability is a Holy Grail that we'll never attain, because the day we do, someone will invent a radical new system that doesn't quite fit our abstraction model...

  • Mod parent up (Score:4, Insightful)

    by dpbsmith ( 263124 ) on Wednesday November 09, 2005 @03:01PM (#13990856) Homepage
    Amen, brother.

    I've been involved in way too many projects where people said, "Oh, yeah, we're doing all our development on Windows but it's no big deal. We aren't going to use anything non-portable."

    Then, when the time came to port it... it was utterly intertwingled with Windows-specific cruft, half of which crept in because nobody even knew they were doing it. If they'd ever tried even once to port it, they could have caught this stuff as it happened.

    I don't mind a conscious decision to use .ini files, or CStrings, or what have you. It was all the non-portable things they did without even knowing it--and the fact that the non-portable stuff was salted and peppered evenly throughout the whole project instead of concentrated in a few well-defined modules--that got to me.

    And it didn't help that everything was compiled with permissive compiler options regarding C/C++ conformance, and a low warning level.
  • Re:Small book (Score:3, Insightful)

    by smackjer ( 697558 ) on Wednesday November 09, 2005 @03:03PM (#13990871) Homepage
    Isn't an OS just a kind of runtime environment? After all, it's just one level of abstraction among many levels of abstraction. A Java Virtual Machine is just one more level of abstraction.
  • by MosesJones ( 55544 ) on Wednesday November 09, 2005 @03:06PM (#13990898) Homepage

    I write portable code, I'm writing on Windows, deploying to an AMD Solaris box and the target is SPARC. I've also done this same "magic" trick on AS/400 and OS/360...

    Now me I just use the tool designed for the job of being platform portable, rather than trying to invent new standard headers (STL anyone?) that address a fraction of the problem.

    Want to do sound on multiple platforms? Graphics? Business Process? Then use Java, its what it was designed to do. And to all those muppets who shout "its not possible"... the deploy has just finished and within 30 seconds its running on two different platforms.
  • by pieterh ( 196118 ) on Wednesday November 09, 2005 @03:10PM (#13990929) Homepage
    My team's been writing 100% portable C code since 1991 or so. We took the same approach that Apache has done since version 2, i.e. build a distinct portability library and remove all non-portable code from the application itself.

    It's amazing anyone would actually write non-portable code except through ignorance. As a programmer, I still run code written in 1991-2 (though it's been marginalised by newer work), and we have made some quite complex products (web servers, code generators) that run on anything that has standard C libraries and BSD-style TCP/IP, including OS/2, OpenVMS, and of course all Unix and Windows boxes.

    The alternative option is to use a VM. Since we write fast system software that's not an option.

    A wise person taught me this over 20 years ago: life is too short to throw out code just because some platform changed. Portability is one of those skills that lets a normal programmer like me accumulate enough quality code over time to become a master programmer.
  • by ajs ( 35943 ) <{ajs} {at} {ajs.com}> on Wednesday November 09, 2005 @03:15PM (#13990977) Homepage Journal
    That's right, you heard me. Don't write portable code.

    Use portable libraries and languages; re-factor your working code to be portable; make high-level choices that support portability (e.g. don't lock yourself in to proprietary solutions), but don't write portable code.

    Why? Because premature portability, like premature optimization is a red herring that steals your attention from the only two things that will ever matter: correctness and maintainability. Write correct code. Write verifiably correct code. Write maintainable code. Do these things and you are done. Then, port it to another platform or ten and optimize the hell out of it. Don't do these things up-front, as they buy you nothing on the first pass, and doing them later will give you the chance to re-consider the structure of your system which you should do at least twice before your first release anyway.

    That said, do not snub portability unduely. If you have the choice of trivially supporting or not supporting portability-enhacing features (e.g. in your choice of a configure/build system), there's no reason not to be portable. Just don't let it set priorities for your project from day one.
  • Re:Small book (Score:2, Insightful)

    by finite_automaton ( 302001 ) on Wednesday November 09, 2005 @03:20PM (#13991009)
    Java doesn't run everywhere: For instance, there is no Java JVM for a Palm

    Hmmmm, IBM and PalmSource might disagree [palmos.com] with you there.

    But you're correct in general. Not every platform you'll want to port code to will have a JVM. And those that do will not have the right JVM

  • by dslauson ( 914147 ) on Wednesday November 09, 2005 @03:28PM (#13991094) Journal
    Yeah, um, that only makes sense in certain contexts

    For example, I work for a company writing embedded software for medical equipment. For testing and QA purposes, we have to target two different architectures: VxWorks for the embedded stuff, and Windows for simulation. From the start, portability has to be a consideration, because code that doesn't port to these two platforms is completely worthless to us, and will have to be rewritten.

    I get your point, and that makes sense when writing desktop apps and plenty of other stuff, but doesn't apply to every situation.
  • Re:Small book (Score:2, Insightful)

    by AKAImBatman ( 238306 ) <akaimbatman@gmaYEATSil.com minus poet> on Wednesday November 09, 2005 @03:38PM (#13991194) Homepage Journal
    For instance, there is no Java JVM for a Palm.

    There [about.com] isn't [palmos.com]? What [sun.com] will [freewarepalm.com] I [sourceforge.net] do [sourceforge.net]?

    Dude. Java is everywhere. It's in tiny little cards [sun.com] and in the latest ARM processors [arm.com]. You can't run. You can't hide. Java will find your OS, and you will be assimilated. Submit to the collective!
  • Re:Cross platform (Score:4, Insightful)

    by Just Some Guy ( 3352 ) <kirk+slashdot@strauser.com> on Wednesday November 09, 2005 @03:43PM (#13991240) Homepage Journal
    How 'bout 'em? Since they have basically nothing whatsoever in common with other "small" systems, I doubt there's much you can do for them besides shooting for POSIX compliance and crossing your fingers.

    Write Once Compile Anywhere will never be completely realized since various systems have incompatible design goals. No matter how portable you've made your command line application, it probably won't make a lot of sense on a Palm. That's not a limitation of your code or PalmOS, but an acknowledgment that they're different animals.

  • Re:Java ??? (Score:4, Insightful)

    by jalet ( 36114 ) <alet@librelogiciel.com> on Wednesday November 09, 2005 @03:46PM (#13991267) Homepage
    > Good Java programmers... use Python [python.org] instead.
  • by Anonymous Coward on Wednesday November 09, 2005 @04:26PM (#13991702)
    I know you're kidding, but unfortunately there are a lot of responses here which indicate that Java is the end-all and be-all of portable programming.

    I'm sorry folks. Such people have never done real cross-platform programming. Java simply isn't an option on MANY platforms. If all you do is x86 platforms, and perhaps some Motorola workstation-class platforms, hey, you're fine.

    But that's not the real world.

    The real world includes MIPS, ARM and other processors. What's more, the embedded world makes up most of the usage of CPU's. Java is not an option in most cases, unless you go out and pay one of the few small shops that will port it a bunch of money. And that assumes your system have the footprint and horsepower to run it.

    I see outfits where the programmers have never programmed on a non-x86 system in their life suddenly wake up to this fact. It would be funny if it wasn't so sad.

    Now, granted the GNU folks are coming up with their Open Source version, and it appears to be coming along fine. But it's not quite there yet.

    Also, keep in mind that it won't run on many platforms, simply because of size limitations. All the world isn't your dual-core 3 GHz x86 with 4 GB of RAM.

    And I'm sorry, but the Java claims that it is as fast as C code just aren't true. Don't believe me? Try doing some benchmarks on 100 MHz small-RAM systems. If you're lucky enough to get it to fit, you'll see that things are just dog slow.

    Most programmers and students today are just too used to working with the supercomputer type of CPU's that the modern desktop is like, and aren't used to dealing with small-footprint type of systems which make up most of the world
  • Indeed... (Score:4, Insightful)

    by Svartalf ( 2997 ) on Wednesday November 09, 2005 @05:49PM (#13992614) Homepage
    I think all the proponents of Java kind of missed the point here. Not everything has or even NEEDS the level of horsepower that Java requires- and just adding more muscle isn't always in the chips.

    And even then, saying "Java" will run everywhere, is really a mis-concept. It'll run most everywhere where there is a least common denominator. I can tell you that while it makes it easier for Oddlabs to make Tribal Trouble for three platforms simultaneously, Java doesn't make it cross-platform- you can't take it and run it on say a Solaris box (though I suspect they could MAKE a Solaris iteration of the game easily enough...) or on an embedded machine using Java, say like the Ignite platform.

    Java makes some things "easier" to make cross-platform, but again, it's like anything else- cross-platform is more of a philosophy than a feature set of a language or toolset. And it's definitely NOT the panacea that the proponents in this thread make it out to be- I should know, I do Java development amongst other things. If you can't make a C++ program at least 40% faster than your best Java code, you might want to re-work the C++ code. This is not to say that it's a bad idea or anything; it's just that people keep trying to jam it into problem sets that it's ill suited for.
  • Re:2 slow (Score:3, Insightful)

    by S.O.B. ( 136083 ) on Wednesday November 09, 2005 @05:57PM (#13992699)
    For enterprise strength app servers WebLogic and WebSphere are written in Java. Not as scalable but getting better is Tomcat also written in Java. The Java application I work on processes about 10 million transactions a week and was ported from the mainframe. The biggest porting headache was not the Java code but SQL differences between DB2 and Oracle. In terms of cost per transaction it's cheaper than many of our mainframe apps.

    For development Eclipse is written in Java using SWT. Eclipse has become the new standard for open IDE tools.

    Up until I lost it in a machine crash I had a Java email program program that I ran using Java 1.1 through to 1.5 on Linux, Windows and MacOS. When was the last time you had a program that didn't even need to be recompiled through 4 major operating system releases let alone as you moved it from OS to OS. Heck, with Linux a 0.0.1 change in the kernal could force a recompile.

    Now, for pure number crunching Java is definitely not the right choice but most apps are not simply churning the CPU. Most real life apps also involve disk I/O, network I/O, etc. If 90% of your app is I/O then it's not going to make much of a difference what technology you use on the other 10%. So why not use Java and make it portable as well.

    With Java, what you gain in portability, isolation from OS releases and a wealth of open source APIs far outweigh the less than 5% (yes it's that little) overhead that the Java runtime adds to 10% of your app.
  • by Anonymous Coward on Wednesday November 09, 2005 @08:12PM (#13993832)
    You have no idea what you're talking about.

    Should me some JVMs which are available for Broadcom's 64-bit processors. That I can buy, right now. According to MIPS, they have Java. Guess what? There are a lot of MIPS CPUs out there which can't run Java, without paying to have it ported.

    Show me some JVMs which are available for Atmel's 32-bit processors.

    You can't.

    For someone who doesn't know what he's talking about, you have a big mouth.
  • by shutdown -p now ( 807394 ) on Thursday November 10, 2005 @06:11AM (#13996361) Journal
    When perl gets generic programming, let me know.
    Most (all?) loosely-typed languages are generic, by definition.

    On a side note, generic programming in C++ is very much a joke. I mean, you cannot even define constraints on template arguments, and have to resort to dirty tricks with delayed instantiation - WTF?

    yes, perl has a lot. No, they're nowhere close to the amount C/C++ has
    The problem with C++ libs is that very few of them adhere to the C++ philosophy, and thus play well with STL and boost. Most are, at best, "C with classes". Quite a few are essentially Java redone in C++ (Qt being the most prominent example). Generic programming, full-scale use of templates, STL-compatible iterators? A rare sight, unfortunately.

    Anyway, if you know a decent cross-platform GUI toolkit written in and for proper C++, please do tell (gtkmm comes close, but it's not really cross-platform enough, and some design decisions, such as ustring, are questionable). I've been looking for that for ages, and got desperate enough that I might well start writing one myself.

UNIX is hot. It's more than hot. It's steaming. It's quicksilver lightning with a laserbeam kicker. -- Michael Jay Tucker

Working...