3.5. Macintosh

I've saved the best for last! :-) Although not the total programmer's environment that Unix is, there are some excellent C compilers available for the Mac. In fact, all of the examples and problem set solutions for this class have been written and tested on my Macintosh, and all of the software I use to format and typeset these course notes (including the handout you're reading now) was written by me, much of it on the Mac. The compiler I use is Think C version 5.1, by Symantec. The rest of this section discusses that compiler, but as far as I know, other Mac compilers (including Codewarrior) are quite similar.

In Think C, at any one time, you're always working with exactly one project, which is Think C's metaphor for the program you're writing. The project is built from one or more sources, including the C source files you'll create and type in. Once you've got things set up correctly, you simply select Run from the Project menu to have all aspects of compiling and running your program taken care of automatically. (You can also ``make'' your program without running it.)

When you first start up Think C, you're presented with a New Project dialog. You can name your project anything you like, but don't add a .c to the end of the name, because as we'll see, we'll be creating individual .c files later which are distinct from the ``project file''. (Later, once you've created a project, you can start up the compiler ``on'' that project by double-clicking on the project file's icon, or you can select Open Project from the Project menu.)

If you've just invoked the compiler for the first time, the default folder where you'll first end up trying to store your own project and source files will probably end up being the compiler's installation directory. This is not ideal, and Symantec recommends against it. Instead, you'll be much better off creating your own folder somewhere else, where you'll keep all your own source files and projects that you've written or typed in, and where they can stay separate from the compiler and all its supporting files.

Step 1: entering or editing your source code

Once you've opened a project, select New from the File menu. This opens a new source file editing window, in which you can begin typing your source code. The source code editing window works like any text editing window, except that it is streamlined and tailored for the typing and editing of C source code. (For example, you may notice that the cursor automatically indents itself on each new line, as the editor guesses the syntax and structure of the C program you're typing.)

Once you've successfully opened a source code editing window, type in the program (if it's an initial example from the class notes or from a book) exactly as shown, including all punctuation, and with the lines laid out just as they are in the code you're typing in. (Later, we'll learn what does and doesn't matter in terms of program layout: where your degrees of freedom are, what the compiler does and doesn't care so much about, which syntax you have to get exactly right and which parts allow you to express some creativity. For now, though, just type everything verbatim.)

At some point, but definitely by the time you're finished typing, you'll need to select a name for the source file, which will be something like hello.c. Select Save As from the File menu, and save the file as normal. You'll want to place it in the same folder as the project file, which ought to be where it ends up by default, but due to the way the Mac's standard open file dialog always seems to remember the last folder you were in for any reason, you'll want to double check that you're still where you want to be before saving files in Think C.

Later, when you have occasion to come back to an existing source file to edit it, you can either open it as usual using Open from the File menu, or else double-click on its name in think C's ``project window'', which is a list of all the sources that make up the current project. (Think C always displays this window whenever a project is open; it's usually pretty small and narrow, and located in the top center or left of the screen.)

Step 2: compiling the program

First, pull down the Edit menu and go to the Options dialog, and run through the various choices. For the simple programs we'll be writing, most of the defaults will be adequate. But do turn off any extensions, ``Think Object'' or C++ or otherwise. (Compiler extensions, no matter how seductive they sound, are unnecessary and would only get in the way, besides intruding on our goal of learning portable C.)

Once you've got a project set up, the easiest way of compiling it is by simply selecting Run from the Project menu, which compiles the program (if necessary) and then runs it, taking you directly to step 3. But if you try that right away, you'll get an error saying that main is undefined, because even though you've typed in a .c file containing your main function, Think C hasn't realized it's an official part of your project yet.

There are at least three ways of adding a source file to the current project:

  1. Pull down Add... from the Source menu. Double-click on the desired file in the upper half of the dialog (or select it and click Add) to place it in the lower list of files to be added. Then click Done.
  2. If the desired source file is the topmost (current) window, select Add (the one without the three dots) from the Source menu.
  3. If the desired source file is the topmost (current) window, select Compile from the Source menu. If the compilation is successful, the source will be automatically added to the project menu.
You can use any one of these methods to add hello.c to the current project. (Generally, I find number 3 most convenient, because it's got a command-key shortcut.)

You're now ready to hit an annoying little feature of Think C, which is that it does not assume any default, initial set of function libraries, ANSI standard or otherwise. This means that the second time you attempt to build or run your program (after fixing the ``main undefined'' problem from when you tried to run a project with no sources at all), you're likely to get error messages telling you that functions with names like printf are undefined, in spite of the fact that your textbook and instructor assured you they would be provided by the compiler vendor. You have to pull down Add... from the Source menu, go to the folder (wherever it is) where you installed Think C, go to the C Libraries subfolder, and select the ANSI library. (In other words, you add it as a second Source to your project. Here, ``source'' just means ``source of information for the project''; it doesn't mean ``C source code''.) (Also, once you've gone over to the C Libraries folder to get the ANSI library, remember to go back to your own C programming folder next time you have occasion to save a file, otherwise it might get lost over in the Think C folder.)

Step 3: running the program

When you select Run from the Project menu, Think C (obviously) runs your program. If your program calls printf to generate any output (as all of our programs will do), a ``console window'' will automatically be opened to display the output. If the program tries to read any input, you can type that input into the same console window.

You can also pull down Build Application from the Project menu. This allows you to turn your program into an actual Mac application. You'll be asked to pick a file name for the application, which will have to be different from both the project file name and any source file (.c) names. (For this reason, it's common to name project files ending in .prj or project or, to be cute, the pi symbol.)

When you create a standalone Mac application, you'll be able to invoke it by double-clicking on its icon, as usual. (However, it probably won't look much like a Mac application once it's running, because assuming it just calls printf, it will still open up the same console window, and it won't have any fancy menus or any dialogs at all.)

Later, when it comes time to write some sample programs which try to read their command line arguments, you will obviously have a problem typing these arguments, because the Mac of course has no command line. Think C does incorporate a mechanism for passing command lines to a program (because even though doing so is entirely un-Mac-like, many C programs, especially those written during introductory classes, still expect them). To get an ``application'' built by Think C to prompt you for a command line, you have to do two things:

  1. Place the line
    	#include <console.h>
    
    at the top of the source file containing your main function, along with the other #include lines.
  2. Define your main function like this:
    	main(int argc, char *argv)
    	{
    	... any local variable declarations go here ...
    
    	argc = ccommand(&argv);
    
    	... rest of program ...
    
In other words, the first executable statement of your main function must be the assignment argc = ccommand(&argv); . The call to the ccommand function causes a dialog box to pop up in which you can type a command line, the contents of which will then be available to the rest of your code through the argc and argv variables, as usual. Also, the dialog gives you a way to redirect the input and output of your program (see the Unix and MS-DOS sections above for a bit more information on input/output redirection). But don't worry about any of this until we get to the topic of command line arguments, towards the end of the class.


Read sequentially: prev up top

This page by Steve Summit // Copyright 1995-9 // mail feedback