Linux Programming Unleashed.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
0172316072 intro 7/26/99 2:18 PM Page 1 Introduction Linux has always provided a rich programming environment, and it has only grown rich- er. Two new compilers, egcs and pgcs, joined the GNU project’s gcc, the original Linux compiler. In fact, as this book went to press, the Free Software Foundation, custodians of the GNU project, announced that gcc would be maintained by the creators and maintain- ers of egcs. A huge variety of editors stand alongside the spartan and much-maligned vi and emacs’ marvelous complexity. Driven largely by the Linux kernel, GNU’s C library has evolved so dramatically that a new version, glibc (also known as libc6) has emerged as the standard C library. Linux hackers have honed the GNU project’s always service- able development suite into powerful tools. New widget sets have taken their place beside the old UNIX standbys. Lesstif is a free, source-compatible implementation of Motif 1.2; KDE, the K Desktop Environment based on the Qt class libraries from TrollTech, answers the desktop challenge posed by the X Consortium’s CDE (Common Desktop Environment). What This Book Will Do for You In this book, we propose to show you how to program in, on, and for Linux. We’ll focus almost exclusively on the C language because C is still Linux’s lingua franca. After introducing you to some essential development tools, we dive right in to system programming, followed by a section on interprocess communication and network programming. After a section devoted to programming Linux’s user interface with both text-based and graphical tools (the X Window system), a section on specialized topics, including shell programming, security considerations, and using the GNU project’s gdb debugger, rounds out the technical discussion. We close the book with three chapters on a topic normally disregarded in programming books: delivering your application to users. These final chapters show you how to use package management tools such as RPM, how to create useful documentation, and discuss licensing issues and options. If we’ve done our job correctly, you should be well prepared to participate in the great sociological and technological phenomenon called “Linux.” Intended Audience Programmers familiar with other operating systems but new to Linux get a solid intro- duction to programming under Linux. We cover both the tools you will use and the environment in which you will be working. 0172316072 intro 7/26/99 2:18 PM Page 2 Linux Programming 2 UNLEASHED Experienced UNIX programmers will find Linux’s programming idioms very familiar. What we hope to accomplish for this group is to highlight the differences you will encounter. Maximum portability will be an important topic because Linux runs on an ever-growing variety of platforms: Intel i386, Sun Sparcs, Digital Alphas, MIPS proces- sors, Power PCs, and Motorola 68000-based Macintosh computers. Intermediate C programmers will also gain a lot from this book. In general, program- ming Linux is similar to programming any other UNIX-like system, so we start you on the path toward becoming an effective UNIX programmer and introduce you to the pecu- liarities of Linux/UNIX hacking. Linux Programming Unleashed, Chapter by Chapter This is not a C tutorial, but you will get a very quick refresher. You will need to be able to read and understand C code and understand common C idioms. Our selection of tools rarely strays from the toolbox available from the GNU project. The reason for this is simple: GNU software is standard equipment in every Linux distribution. The first seven chapters cover setting up a development system and using the standard Linux development tools: • gcc • make • autoconf • diff • patch • RCS • emacs The next section introduces system programming topics. If you are a little rusty on the standard C library, Chapter 9 will clear the cobwebs. Chapter 10 covers Linux’s file manipulation routines. Chapter 11 answers the question, “What is a process?” and shows you the system calls associated with processes and job control. We teach you how to get system information in Chapter 12, and then get on our editorial soapbox in Chapter 13 and lecture you about why error-checking is A Good Thing. Of course, we’ll show you how to do it, too. Chapter 14 is devoted to the vagaries of memory management under Linux. 0172316072 intro 7/26/99 2:18 PM Page 3 3 INTRODUCTION We spend four chapters on various approaches to interprocess communication using pipes, message queues, shared memory, and semaphores. Four more chapters show you how to write programs based on the TCP/IP network protocol. After a general introduc- tion to creating and using programming libraries in Chapter 24 (including the transition from libc5 to libc6), we cover writing device drivers and kernel modules in Chapter 25, because considerable programming energy is spent providing kernel support for the latest whiz-bang hardware device or system services. User interface programming takes up the next eight chapters. Two chapters cover charac- ter-mode programming; first the hard way with termcap and termios, and then the easi- er way using ncurses. After a quick introduction to X in Chapter 28, Chapter 29 focuses on using the Motif and Athena widget sets. Programming X using the GTK library is Chapter 30’s subject, followed by Qt (the foundation of KDE) in Chapter 31, and Java programming in Chapter 32. For good measure, we also cover 3D graphics programming using OpenGL. The next section of the book covers three special-purpose topics. Chapter 34 examines bash shell programming. We deal with security-related programming issues in Chapter 35, and devote Chapter 36 to debugging with gdb. The book ends by showing you the final steps for turning your programming project over to the world. Chapter 37 introduces you to tar and the RPM package management tool. Documentation is essential, so we teach you how to write man pages and how to use some SGML-based documentation tools in Chapter 38. Chapter 39, finally, looks at the vital issue of software licensing. 0172316072 intro 7/26/99 2:18 PM Page 4 4 0272316072 part1 7/26/99 2:38 PM Page 5 The Linux Programming Toolkit PART I IN THIS PART • Overview 7 • Setting Up a Development System 13 • Using GNU cc 39 • Project Management Using GNU make 53 • Creating Self-Configuring Software with autoconf 65 • Comparing and Merging Source Files 85 • Version Control with RCS 103 • Creating Programs in Emacs 115 0272316072 part1 7/26/99 2:38 PM Page 6 0372316072 CH01 7/26/99 2:01 PM Page 7 Overview by Kurt Wall 1 INTHISCHAPTER •The Little OS That Did 8 •The Little OS That Will 8 •A Brief History of Linux 9 •Linux and UNIX 9 •Programming Linux 10 •Why Linux Programming? 10 HAPTER C 0372316072 CH01 7/26/99 2:01 PM Page 8 The Linux Programming Toolkit 8 PART I Linux has arrived, an astonishing feat accomplished in just over eight years! 1998 was the year Linux finally appeared on corporate America’s radar screens. The Little OS That Did It began in March 1998, when Netscape announced that they would release the source code to their Communicator Internet suite under a modified version of the GNU project’s General Public License (GPL). In July, two of the world’s largest relational database ven- dors, Informix and Oracle, announced native Linux ports of their database products. In August, Intel and Netscape took minority stakes in Red Hat, makers of the market- leading Linux distribution. IBM, meanwhile, began beta testing a Linux port of DB/2. Corel Corporation finally ported their entire office suite to Linux and introduced a line of desktop computers based on Intel’s StrongARM processor and a custom port of Linux. These developments only scratch the surface of the major commercial interest in Linux. Note As this book went to press, Red Hat filed for an initial public offering (IPO) of their stock. It is a delicious irony that a company that makes money on a free operating system is going to become a member of corporate America. I would be remiss if I failed to mention Microsoft’s famous (or infamous) Halloween documents. These were leaked internal memos that detailed Microsoft’s analysis of the threat Linux posed to their market hegemony, particularly their server operating system, Windows NT, and discussed options for meeting the challenge Linux poses. The Little OS That Will As a server operating system, Linux has matured. It can be found running Web servers all over the world and provides file and print services in an increasing number of busi- nesses. An independent think tank, IDG, reported that Linux installations grew at a rate of 212 percent during 1998, the highest growth rate of all server operating systems including Windows NT. Enterprise-level features, such as support for multi-processing and large file-system support, continue to mature, too. The 2.2 kernel now supports up to sixteen processors (up from four in the 2.0 series kernels). Clustering technology, known as Beowulf, enables Linux users to create systems of dozens or hundreds of inexpensive, commodity personal computers that, combined, crank out supercomputer level process- ing speed very inexpensively compared to the cost of, say, a Cray, an SGI, or a Sun. 0372316072 CH01 7/26/99 2:01 PM Page 9 Overview 9 CHAPTER 1 On the desktop, too, Linux continues to mature. The KDE desktop provides a GUI that 1 rivals Microsoft Windows for ease of use and configurability. Unlike Windows, however, KDE is a thin layer of eye candy on top of the operating system.