Early Praise for Resourceful Code Reuse Resourceful Code Reuse offers readers a quick way to understand how code is compiled and linked using the C language within the modern context of consuming and processing JSON. ➤ Mike Riley President, Ingenious Solutions, Inc. Every developer should know about the concepts described in this book. Even if you don’t use C and Python in your daily work, you will learn something about software development and how to reuse code. ➤ Dominik Hauser Author of Build Location-Based Projects for iOS Everyone can code, but writing beautiful code requires code reuse skills. Whether you are starting coding or you have broad experience, this book offers a practical guide that allows you to optimize your code by applying code reuse techniques. ➤ Jose Arturo Mora Soto Senior Curriculum Engineer, 2U Inc. We've left this page blank to make the page numbers the same in the electronic and paper books. We tried just leaving it out, but then people wrote us to ask about the missing pages. Anyway, Eddy the Gerbil wanted to say “hello.” Resourceful Code Reuse Write → Compile → Link → Run Dmitry Zinoviev The Pragmatic Bookshelf Raleigh, North Carolina Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. For our complete catalog of hands-on, practical, and Pragmatic content for software devel- opers, please visit https://pragprog.com. The team that produced this book includes: CEO: Dave Rankin COO: Janet Furlow Managing Editor: Tammy Coron Development Editor: Adaobi Obi Tulton Copy Editor: Corina Lebegioara Founders: Andy Hunt and Dave Thomas For sales, volume licensing, and support, please contact [email protected]. For international rights, please contact [email protected]. Copyright © 2021 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. ISBN-13: 978-1-68050-820-8 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0—April 2021 Contents Acknowledgments . . . . . . . . . . . vii Preface . . . . . . . . . . . . . . ix Introduction: Why Reuse Code? . . . . . . . . xi 1. Reuse Code at Compile Time (C and Python) . . . . . 1 Arranging Source and Header Files (the C Way) 1 Modularizing Code (the Python Way) 9 2. Reuse Code at Link Time (C Only) . . . . . . . 13 Compiling Object Files 13 Building Static Libraries 21 Building Dynamic Libraries 29 3. Reuse Code at Runtime (C and Python) . . . . . . 35 Harnessing Dynamic Loading 35 Getting a Taste of Remote Procedure Calls 40 Bibliography . . . . . . . . . . . . . 49 Acknowledgments This is my second book edited by Adaobi Obi Tulton and, just like the first book, it would not be possible without her enthusiasm, dedication, and highest professionalism. Thank you, Adaobi. I am grateful to my reviewers (in alphabetical order): Ludovico Fischer, Dominik Houser, Andy Lester, Mike Riley, Jose Arturo Mora Soto, and Ilya Usvyatsky. Their criticism was refreshening and energizing. Their reviews greatly improved the structure, usability, and style of the book. Thank you, reviewers. My wife, Anna; my children, Eugenia and Roman; my colleagues from Suffolk University (especially Pelin Bicen) and my friends (especially Dmitry and Tanya Cherevik) provided much-needed emotional support. Thank you, my support- ers. Last but not least, I am grateful to Desbenoit thenounproject.com/term/reuse/6786/ for the royalty-free image on page 47. report erratum • discuss Preface I took my first and only academic course in software engineering in the fall of 1994. The course was taught by Professor Peter Henderson, who had spent a quarter of a century at Stony Brook University and retired shortly after my graduation. The course was taught in Smalltalk, the mother of all object-ori- ented languages, and was undeniably excellent. Sadly, I have never had another chance to write a line of code in Smalltalk and barely remember what it looked like. What I do remember is the mantra that Professor Henderson recited over and over again, until it got imprinted into my mind, from the top of my head to my fingertips: “Thou shalt use make files and reuse your code.” Ever since, I’ve started every new non-trivial project by writing a makefile and spending a ridiculous amount of time dissecting the job into potentially reusable units. In this book, I want to share with you my passion for code reuse and code organization and the skills that support that passion. About the Reader This book is mainly intended for beginner through intermediate software developers in C and, to a much lesser extent, in Python, who want to achieve higher productivity, better code quality, and more flexible and adaptable products by reusing and organizing previously written code. Experience with command-line software development tools is helpful but not required. About the Book After the mandatory introduction that sets the scene for the story, the book goes over three stages of small project development: editing (producing the project’s text in a programming language), compiling (converting the text into object files with machine code), and linking (combining the pre-compiled object files into one executable program file). Any unit produced at any stage can be reused and shared at a later stage, including the runtime when there is no more development. The rest of the book is organized into three chapters report erratum • discuss Preface • x focused on these topics: compile-time reuse, link-time reuse, and runtime reuse. I tried to keep the chapters as independent as possible but still ended up with some backward and forward references. The forward references are not so essential for understanding the material, but if you open the book in the middle and come across a backward reference that you do not recognize, I strongly suggest visiting it. About the Software To compile and run the C examples mentioned in the book, you need a decent C compiler (gcc is the best, but Intel and Microsoft would probably work, too) and a set of C development tools: maker (make), linker (ld), file, strip, ldd, and ranlib. The GNU development toolset works marvels; other toolsets may or may not work. All examples in the book have been tested on a Linux computer but will most likely work on macOS. For the Python examples, a Python-3.x interpreter (python) is all you want. No third-party modules are required. Shall we start? Dmitry Zinoviev [email protected] February 2021 report erratum • discuss