Nextstep Programming
Total Page:16
File Type:pdf, Size:1020Kb
To my parents, Marvin and Marian, who have put up with my addiction to computers since before I could type. - Simson To Louise, Nina, and Timmy. - Michael vii Preface Welcome! This book is about programming computers running NeXTSTEP. It’s a no- nonsense, hands-on book that teaches programmers how to write applica- tion programs that take full advantage of NeXTSTEP, the operating envi- ronment from NeXT Computer Inc. Writing programs for NeXTSTEP is fundamentally different than writing programs for other computers, because NeXTSTEP represents a radical departure from conventional programming environments. One writes NeXTSTEP programs by building systems of related but distinct parts, or objects, and connecting them together to form an integrated whole. Confin- ing different aspects of a program to different pieces makes those pieces easier to design, implement, debug, and reuse. This is what is known as object-oriented programming. NeXTSTEP embodies the principles of object oriented programming from its user interface down to its very core. This greatly simplifies the task of interfacing application programs with the NeXTSTEP operating environ- ment. The downside is that it makes the NeXTSTEP environment very dif- ferent from the environments to which most programmers are accustomed: there’s a steep curve to climb when learning to program in this easy-to-pro- gram environment (sounds strange, but it’s true). We wrote this book out of frustration: at the time, there was no single book that explained step-by-step how to write programs for NeXTSTEP. Instead, a programmer trying to approach the platform was confronted by the NeXT technical documentation and the source-code for several dozen example programs. There was no mention of style, program structure or design con- siderations. It seemed that there was a black art to writing programs for the black computer, along with a priesthood which promised that newcomers might one day become members – once we paid our dues. With this book, we’ve paid a lot of your dues for you. This book is for anybody who knows how to program in C and wants to do something substantial with NeXTSTEP. Our goal is to get you up-and-run- ning as quickly as possible. This isn’t a textbook or a reference manual – Brad Cox (the inventor of Objective-C) and the technical writing depart- ment at NeXT have already written those. This book is instead a jumping- off point. Consequently, we will occasionally sacrifice depth for breadth viii Preface and brevity. You won’t learn all of the options of Interface Builder or all the obscure methods that the Text object implements by reading this book. But you’ll acquire the conceptual framework necessary to understand NeXT’s excellent documentation, enabling you to discover what you need to know about the Text object and everything else. All of the examples in this book were developed and tested under both ver- sion 2.1 and 3.0 of the NeXTSTEP operating environment. (When we write NeXTSTEP 2.1 this includes NeXTSTEP 2.0, 2.1, and 2.2.) Although NeXTSTEP 3.0 offers many features not found in the 2.1 operating system, from the programmer’s point of view the two releases are actually quite similar. Furthermore, we’ve tried to limit the subject matter of this book to those underlying concepts and features of the operating system – the parts that haven’t changed much since the initial release of NeXTSTEP 1.0 in 1989. One significant difference between NeXTSTEP 2.1 and NeXTSTEP 3.0, however, lies with Interface Builder, the powerful development tool for building application interfaces and application projects. With NeXTSTEP 3.0, NeXT split the 2.1 version of Interface Builder into two distinct pro- grams: Interface Builder 3.0 and Project Builder. Nevertheless, the two 3.0 tools contain essentially the same functionality as the single 2.1 Interface Builder tool. To help deal with this difference, we’ve explained how to use Interface Builder under both 2.1 and 3.0. We’ve also covered explanatory informa- tion about Project Builder, where appropriate. What is NeXTSTEP? NeXTSTEP is the object-oriented environment that runs on NeXT, 80486- based, and possibly other computers. It is the main reason that the NeXT computer is different, both for users and programmers. From the user’s point of view, NeXTSTEP is the unified graphical operat- ing environment that makes a computer running it easy to use: • Display PostScript, a version of Adobe’s PostScript imaging system that displays information on a computer’s screen. • Workspace, NeXTSTEP’s graphical interface to the UNIX file system. • Mail, Digital Librarian, the Edit editor, and other NeXT-supplied appli- cation programs. What You Need ix • The Preferences and Defaults system, which lets users store their pref- erences for individual programs without having to directly modify spe- cial files stored in their home directories. From the programmer’s point of view, NeXTSTEP includes: • The NeXT Application Kit, a collection of Objective-C objects which gives NeXTSTEP its distinctive look-and-feel. • The NeXTSTEP Sound and Music Kits, for writing programs that make noise. • The NeXTSTEP Database Kit, DBKit, for writing programs that access databases. • The streams package, which allows interprocess communication, archiving of information, and direct mapping of files. • The C threads package, for writing multi-threaded applications. • The Mach operating system. Using NeXTSTEP gives your program the characteristic NeXTSTEP look- and-feel. Although every application program is different, NeXTSTEP makes it easier for all applications to work in similar ways, which in turn makes it easier for people to learn new application programs. NeXTSTEP also makes it easier for application programs to work with each other. Because of its object-oriented nature, NeXTSTEP applications can easily provide services and special functions to other application pro- grams running on the same computer or across the network. Using NeXTSTEP also speeds your development time. Programmers who have gone through NeXT’s Developer’s Training Camp report that they can develop an application three to ten times faster under NeXTSTEP than under Microsoft Windows or X Windows. Indeed, it is so easy to develop an application under NeXTSTEP that many large companies are purchas- ing NeXT computers specifically to develop their own custom, in-house applications: the increased programmer productivity justifies the purchase decision! What You Need In order to try out the examples in this book, you will need a computer run- ning the Extended Edition of the NeXTSTEP Software Release 2.0, or later. The Extended Edition contains everything you need to develop a x Preface NeXTSTEP application, including the NeXT’s GNU C compiler (devel- oped by the Free Software Foundation), the assembler, linker, all of the libraries, and all of the “include” files (files that are included into your C program with the #include and #import directives). The Extended edition also contains on-line documentation, a variety of demonstration programs, source-code for many of these programs, and a variety of other tools, such as the TeX document processing system and the complete works of Will- iam Shakespeare. If you have a NeXTcube or a NeXTstation with a 400MB hard disk or larger, you’ve already got the Extended Edition: it comes installed as stan- dard software on your system. If you have a NeXTstation with a 105MB hard disk, you’re out of luck: there’s just no way to make the files and programs that you need for the development environment fit onto this hard disk and still have room left to do anything useful. Your best bet is to buy a 400MB (or larger) external hard disk, put the Extended Edition on it, and boot it as your main disk: use the internal hard disk for backup and for virtual memory swapping space. If you have a NeXTstation with a 200MB or 250MB hard disk, you can copy the individual files that you need to develop programs and still have some breathing space left over to do some serious work. Don’t worry about copyright restrictions: the Standard Edition includes a license for the Extended Edition. Just copy the files from a friend or a friendly computer store. The files are also on the NeXTSTEP 3.0 CD-ROM. If you are using an Intel 486-based computer with NeXTSTEP 486, then you must specially purchase the Developer’s Edition if you wish to write programs. Programming with NeXTSTEP 2.1 Below we list the directories and files that you need for changing the NeXTSTEP 2.1 Standard Edition into something you can program with. Directories: /lib, /usr/lib/nib, /usr/include Files (programs) in /bin: ar file ld segedit as g++filt What You Need xi nm size cc gdb nm++ strip cc++ kgdb otool ebadexec ranlib pswrap kl_ld 1 Library Files: /usr/lib/*.a Note: UNIX libraries are files that end with the extension .a. If you copy a library with the cp command, you will need to run ranlib on the library to “refresh” the date stored in it. Programming with NeXTSTEP 3.0 With NeXTSTEP 3.0, many of the special files and directories that you need to write programs have been moved to the /NextDeveloper directory. Symbolic links have been left in their place for backwards compatibility. For example, the directory /usr/include is now a symbolic link to /NextDe- veloper/Headers. This change makes it substantially easier to do develop- ment on a NeXTSTEP 3.0 system with a small hard disk: Simply make the /NextDeveloper directory on your hard disk be a symbolic link to the same directory on the CD-ROM drive containing the NeXTSTEP 3.0 release.