Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 85 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
85
Dung lượng
6,76 MB
Nội dung
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 1 Linux Programming U NLEASHED 2 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 2 INTRODUCTION 3 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 3 4 0172316072 intro 7/26/99 2:18 PM Page 4 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 5 0272316072 part1 7/26/99 2:38 PM Page 6 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 1 CHAPTER Overview by Kurt Wall 0372316072 CH01 7/26/99 2:01 PM Page 7 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. The Linux Programming Toolkit P ART I 8 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 8 On the desktop, too, Linux continues to mature. The KDE desktop provides a GUI that 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. The powerful com- mand-line interface is never more than one click away. Indeed, as this book went to press, Caldera Systems released version 2.2 of OpenLinux, which contained a graphical, Windows-based installation procedure! No less than four office productivity suites exist or will soon be released: Applixware, Star Office, and Koffice, part of the KDE project, are in active use. Corel is finishing up work on their office suite, although WordPerfect 8 for Linux is already available. On top of the huge array of applications and utilities avail- able for Linux, the emergence of office applications every bit as complete as Microsoft Office establishes Linux as a viable competitor to Windows on the desktop. A Brief History of Linux Linux began with this post to the Usenet newsgroup comp.os.minix, in August, 1991, written by a Finnish college student: Hello everybody out there using minix- I’m doing a (free) operating system (just a hobby, won’t be big and professional like gnu) for 386(486) AT clones. That student, of course, was Linus Torvalds and the “hobby” of which he wrote grew to what is known today as Linux. Version 1.0 of the kernel was released on March 14, 1994. Version 2.2, the current stable kernel release, was officially released on January 25, 1999. Torvalds wrote Linux because he wanted a UNIX-like operating system that would run on his 386. Working from MINIX, Linux was born. Linux and UNIX Officially and strictly speaking, Linux is not UNIX. UNIX is a registered trademark, and using the term involves meeting a long list of requirements and paying a sizable amount of money to be certified. Linux is a UNIX clone, a work-alike. All of the kernel code was written from scratch by Linus Torvalds and other kernel hackers. Many programs that run under Linux were also written from scratch, but many, many more are simply ports of software from other operating systems, especially UNIX and UNIX-like operat- ing systems. More than anything else, Linux is a POSIX operating system. POSIX is a family of stan- dards developed by the Institute of Electrical and Electronic Engineers (IEEE) that define a portable operating system interface. Indeed, what makes Linux such a high quality UNIX clone is Linux’s adherence to POSIX standards. Overview C HAPTER 1 9 1 OVERVIEW 0372316072 CH01 7/26/99 2:01 PM Page 9 Programming Linux As Linux continues to mature, the need for people who can program for it will grow. Whether you are a just learning to program or are an experienced programmer new to Linux, the array of tools and techniques can be overwhelming. Just deciding where to begin can be difficult. This book is designed for you. It introduces you to the tools and techniques commonly used in Linux programming. We sincerely hope that what this book contains gives you a solid foundation in the practical matters of programming. By the time you finish this book, you should be thoroughly prepared to hack Linux. Why Linux Programming? Why do people program on and for Linux? The number of answers to that question is probably as high as the number of people programming on and for Linux. I think, though, that these answers fall into several general categories. First, it is fun—this is why I do it. Second, it is free (think beer and speech). Third, it is open. There are no hidden interfaces, no undocumented functions or APIs (application programming interfaces), and if you do not like the way something works, you have access to the source code to fix it. Finally, and I consider this the most important reason, Linux programmers are part of a special community. At one level, everyone needs to belong to something, to identify with something. This is as true of Windows programmers as it is of Linux programmers, or people who join churches, clubs, and athletic teams. At another, more fundamental level, the barriers to entry in this community are based on ability, skill, and talent, not money, looks, or who you know. Linus Torvalds, for example, is rarely persuaded to change the kernel based on rational arguments. Rather, working code persuades him (he often says “Show me the code.”). I am not supposing or proposing that Linux is a meritocracy. Rather, one’s standing in the community is based on meeting a communal need, whether it is hacking code, writ- ing documentation, or helping newcomers. It just so happens, though, that doing any of these things requires skill and ability, as well as the desire to do them. As you participate in and become a member of Linux’s programming community, we hope, too, that you will discover that it is fun and meaningful as well. I think it is. In the final analysis, Linux is about community and sharing as much as it is about making computers do what you want. The Linux Programming Toolkit P ART I 10 0372316072 CH01 7/26/99 2:01 PM Page 10 [...]...0372 316 072 CH 01 7/26/99 2: 01 PM Page 11 Overview CHAPTER 1 Summary 1 OVERVIEW This chapter briefly recounted Linux s history, took a whirlwind tour of the state of Linux and Linux programming today, and made some reasonable predictions about the future of Linux In addition, it examined Linux s relationship to UNIX and took a brief, philosophical look at why you might find Linux programming appealing 11 ... patch has one hunk which spans # lines 1 through 7 # # the following patch was made like this: # cp Makefile Makefile.orig # emacs Makefile # diff -u Makefile.orig Makefile # beware of mangled whitespace (especially tabs) when # cutting and pasting patch Makefile . 8 •A Brief History of Linux 9 Linux and UNIX 9 Programming Linux 10 •Why Linux Programming? 10 1 CHAPTER Overview by Kurt Wall 0372 316 072 CH 01 7/26/99 2: 01 PM Page 7 Linux has arrived, an astonishing. want. The Linux Programming Toolkit P ART I 10 0372 316 072 CH 01 7/26/99 2: 01 PM Page 10 Summary This chapter briefly recounted Linux s history, took a whirlwind tour of the state of Linux and Linux programming. 1 11 1 OVERVIEW 0372 316 072 CH 01 7/26/99 2: 01 PM Page 11 12 0372 316 072 CH 01 7/26/99 2: 01 PM Page 12 IN THIS CHAPTER • Hardware Selection 14 • Processor/Motherboard 15 • User Interaction Hardware: