SPAM_02.book Page ii Tuesday, May 31, 2005 3:33 PM WPC_02.book Page i Friday, June 10, 2005 12:06 PM WRITE PORTABLE CODE WPC_02.book Page ii Friday, June 10, 2005 12:06 PM WPC_02.book Page iii Friday, June 10, 2005 12:06 PM WRITE PORTABLE CODE An Introduction to Developing Software for Multiple Platforms by Brian Hook San Francisco WPC_02.book Page iv Friday, June 10, 2005 12:06 PM WRITE PORTABLE CODE. Copyright © 2005 by Brian Hook. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. Printed on recycled paper in the United States of America 1 2 3 4 5 6 7 8 9 10 – 07 06 05 04 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Publisher: William Pollock Managing Editor: Karol Jurado Production Manager: Susan Berge Cover and Interior Design: Octopod Studios Developmental Editors: William Pollock, Elizabeth Zinkann Technical Reviewer: Tom Forsyth Copyeditor: Marilyn Smith Compositor: Riley Hoffman Proofreader: Stephanie Provines Indexer: Kevin Broccoli For information on book distributors or translations, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 555 De Haro Street, Suite 250, San Francisco, CA 94107 phone: 415.863.9900; fax: 415.863.9950; [email protected]; http://www.nostarch.com The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it. Library of Congress Cataloging-in-Publication Data Hook, Brian, 1971- Write portable code : an introduction to developing software for multiple platforms / Brian Hook.-- 1st ed. p. cm. Includes bibliographical references and index. ISBN 1-59327-056-9 1. Software compatibility. 2. Computer software--Development. I. Title. QA76.76.C64H66 2005 005.1--dc22 2005012981 WPC_02.book Page v Friday, June 10, 2005 12:06 PM For my wife, Jennifer WPC_02.book Page vi Friday, June 10, 2005 12:06 PM WPC_02.book Page vii Friday, June 10, 2005 12:06 PM B R I E F C O N T E N T S Preface.........................................................................................................................xv Acknowledgments.........................................................................................................xix Introduction: The Art of Portable Software Development.......................................................1 Chapter 1: Portability Concepts........................................................................................7 Chapter 2: ANSI C and C++..........................................................................................19 Chapter 3: Techniques for Portability...............................................................................25 Chapter 4: Editing and Source Control............................................................................63 Chapter 5: Processor Differences....................................................................................77 Chapter 6: Floating Point...............................................................................................91 Chapter 7: Preprocessor..............................................................................................109 Chapter 8: Compilers..................................................................................................119 Chapter 9: User Interaction..........................................................................................141 Chapter 10: Networking..............................................................................................149 Chapter 11: Operating Systems....................................................................................155 Chapter 12: Dynamic Libraries.....................................................................................175 Chapter 13: Filesystems...............................................................................................187 Chapter 14: Scalability................................................................................................197 Chapter 15: Portability and Data..................................................................................201 Chapter 16: Internationalization and Localization...........................................................207 Chapter 17: Scripting Languages..................................................................................213 Chapter 18: Cross-Platform Libraries and Toolkits............................................................219 Appendix A: POSH.....................................................................................................223 Appendix B: The Rules for Portability.............................................................................229 References..................................................................................................................233 Index.........................................................................................................................235 WPC_02.book Page viii Friday, June 10, 2005 12:06 PM C O N T E N T S I N D E T A I L PREFACE xv This Book’s Audience ..............................................................................................xvi Online Resources ..................................................................................................xviii ACKNOWLEDGMENTS xix INTRODUCTION THE ART OF PORTABLE SOFTWARE DEVELOPMENT 1 The Benefits of Portability ..........................................................................................2 Elements of the Platform ............................................................................................3 The Problem with Assumptions ...................................................................................4 Coding Standards ....................................................................................................4 The Framework for Portable Programming ...................................................................5 1 PORTABILITY CONCEPTS 7 Portability Is a State of Mind, Not a State ....................................................................7 Develop Good Portability Habits ................................................................................8 Good Habits Trump Specific Knowledge of Bugs or Standards ......................................9 Port Early and Port Often ..............................................................................9 Develop in a Heterogeneous Environment ......................................................9 Use a Variety of Compilers .........................................................................10 Test on Several Platforms ............................................................................10 Support Multiple Libraries ...........................................................................11 Plan Portability for a New Project .............................................................................11 Make Portability Easy ................................................................................11 Choose a Reasonable Level of Portability .....................................................12 Don’t Wed Your Project to Proprietary Products ............................................14 Port Old Code .......................................................................................................16 Assume Code Is Not Portable Until It Has Been Ported ...................................16 Modify Only the Bare Essentials ..................................................................16 Plan Your Attack .......................................................................................17 Document Everything in Revision Control ......................................................17 2 ANSI C AND C++ 19 Why Not Another Language? ..................................................................................19 C and C++ Provide Low-Level Access ..........................................................20 C and C++ Compile to Native Code ...........................................................20 C and C++ Dialects................................................................................................21 Portability and C/C++ ............................................................................................22