MPW for Newbies

 
Updated 23 Mar. '99

A Quick Intro

Apple now has their MPW 'development environment' free for the downloading. MPW supports C and C++, among other languages. No doubt there are many, many cool features of MPW that make it great for massive projects, but all I needed for the moment was a way to write the introductory 'hello world' program. The lack of information about how to download MPW and get up to speed using it to build simple C programs motivated me to write this document, hopefully to save others the time, energy and frustration I spent piecing it all together.

I hope to cover everything you need to go from just 'having a mac' to being able to write simple C programs using MPW. This document is a combination between a 'how-to' and a recount of my experiences. It probably tells you some right things for the wrong reasons, I'm just learning this stuff. I only hope it doesn't tell you any 'wrong' things... If you're already savvy about MPW and I've blown it here horribly, please let me know.

I can't promise you that you won't be downloading things you don't need or that you won't miss something that you will need further down the line, but if you are new to programming or new to mac programming and you want to start with 'hello world' in C on the mac for free, this is the info to get you rolling. I wish something like this document existed when I wanted to get started with MPW!

Part 1 -- Download a bunch of stuff!

Go to ftp://ftp.apple.com/developer/Tool_Chest/Core_Mac_OS_Tools/MPW_etc./MPW-GM/. I hate it when browsers open new windows on me, but I can see a use for it in this situation, so here's a link to open the FTP site in a new window.

Start with the directory 'MPW_GM'. GM stands for 'Gold Master', it's the one you want. PR either stands for 'previous release' or 'pre-release'. Either way, don't bother with the MPW_PR directory. The only reason I didn't say to go to MPW_GM in the first place on those links above is because I wanted you to see where the Documentation is kept, but don't bother downloading any doc just yet.

Underneath MPW_GM, you'll see 'Interfaces&Libraries' and 'MPW'. Start with MPW, get the file MPW_Shell.sit.hqx. This contains the application MPW Shell, which is what you run to do all this stuff, and it also contains some folders where you'll put other stuff that you download seperately. As of this writing, MPW is version 3.4.2 and all the tools that go with it should be marked 'E.T.O #23'.

In this same directory, there's a subdirectory called 'Tools'. I started with only a few and based on error messages from MPW, I was able to figure out what _else_ I needed. Bookmark this tools archive, you'll be back here a lot.

The first tool I figured out that I needed was MrC, that's a C compiler. There's also 'MrCpp', the C++ compiler if that's what you're after. We'll get back to downloading more tools a bit later.

Go back up one level and then down into the 'Interfaces&Libraries' folder. Here we've got four subdirectories; 'DebuggingLibraries', 'Interfaces', 'Libraries' and 'RuntimeLibraries'. The stuff under 'DebuggingLibraries' has all the same names as the stuff under 'Libraries', but I figure until I know more about programming on the mac, the DebuggingLibraries are likely to add confusion.

Out of the Interfaces directory, download the files CIncludes.sit.hqx and RIncludes.sit.hqx. There might be more I'll need later, but I needed both of those to get 'hello world' working the way I wanted.

Out of the Libraries directory, download the four files:

  • CLibraries.sit.hqx
  • Libraries.sit.hqx
  • PPCLibraries.sit.hqx
  • SharedLibraries.sit.hqx
That may be too much. I'm sure that I needed at least CLibraries and SharedLibraries to get 'hello world' to work, though.

Also look in RuntimeLibraries and grab Required_for_MPW.sit.hqx, but only if you are running OS version 7.6 or earlier.

OK, so now I thought I had it all, but this is where I started running into errors from MPW that told me there were more tools that I needed. This is where that bookmark to the MPW tools archive comes in handy...

At a bare minimum, you'll need the following files out of that Tools directory:

  • Commando.sit.hqx
  • CreateMake.sit.hqx
  • Link.sit.hqx
  • Make.sit.hqx
  • MrC.sit.hqx
  • Rez.sit.hqx
  • SC.sit.hqx

I was really close now, but there were a couple more things about MPW that I still was missing in order to compile and test my code like I was used to doing in a UNIX shell.

Assuming you've downloaded all that stuff above, it's time for...

Part 2 -- Organizing all this stuff on your disk!

The folder that gets created when you've downloaded MPW_Shell.sit.hqx is named 'MPW_Shell.sit.hqx Folder'. I renamed mine 'MPW' and put it on the hard drive partition for my school work. It's going to get plenty big after you put all the other stuff you downloaded into it, but disk space is cheap these days so I'll assume that's not an issue.

Create a sub-folder of MPW called Interfaces. Put into it the folders 'CIncludes' and 'RIncludes' that you downloaded from MPW_GM/Interfaces&Libraries/Interfaces page.

Create a sub-folder of MPW called Libraries. Put into it the folders 'CLibraries', 'Libraries', 'PPCLibraries' and 'SharedLibraries' that you downloaded from the MPW_GM/Interfaces&Libraries/Libraries page.

There should already be sub-folders of MPW named 'User Commands' and 'Scripts'. Both of these folders are automatically searched by the MPW Shell when you give it commands. For example, the command 'MrC hello.c' should compile the program 'hello.c', but only if the MPW Shell knows where to find the command 'MrC'. MrC was the first tool I downloaded, the C compiler. I first put the 'MrC' file (it shows up as kind 'MPW Tool') in the same folder as the MPW Shell application and the command 'MrC hello.c' gave me an error that told me that it really did try to compile my C program (it complained that it couldn't find 'stdio.h', that was before I knew about the stuff that needed to be in the 'Interfaces' folder). I don't know whether the MPW tool 'MrC' belongs in 'User Commands' or 'Scripts', or if it even matters. After a while, I got tired of all those MPW tools I kept downloading cluttering up my MPW folder. As soon as I figured out that the shell searched those two folders for commands, I moved all my MPW tools into 'Scripts' (saving 'User Commands' for any tools I end up building myself). I'm sure there's a way to add or change the folders MPW searches for commands, but I don't want to get into that now.

As for the stuff you downloaded in the 'Required for MPW' folder, that you'll need to figure out yourself based on what OS you are running. They're all system extensions and should be installed like any other Macintosh system extension. Things get weird if you're using an OS version 7.5 or later, but earlier than 8.0, as I am. I have to have this file StdCLib in the same folder as the things I build... I don't really want to open that can of worms, I've already opened a pretty big one here just trying to explain how to build a 'hello world' program with MPW.

Now that you've put everything in its place, you can hit the big time with...

Part 3 -- Creating, compiling, building and running the C program 'hello world'

When you launch the MPW Shell, it opens up a window called the 'Worksheet' and there's already a bunch of stuff in it. That's help information which you should look at sooner or later, but don't worry about it for now. The worksheet window reminds me a little bit of the Listener window in Lisp, or of the UNIX shell. MPW was created by UNIX-weenies, so the analogy holds for the most part.

The first thing I learned about MPW from the documentation (I haven't said much about downloading doc, you'll want to at some point, but I hope what I write here gets you to the point where you can _use_ the doc rather than be frustrated by the fact that it doesn't tell you what I'm telling you here) is that the 'Return' key on the keyboard moves the cursor down to the next line and that's it. To actually execute a command, you have to use the 'Enter' key. 99.9% of the time, the two keys have the same function in most apps, but in MPW, they are different.

But anyway, we're writing 'hello world' here, so create a new file. It asks you to name the file when you create it, 'hello.c' is fine. Unlike many C development environments, it seems that you don't need to end your C code files with a '.c' extension. Since we're writing 'hello world' in C, the file should end up looking like the following (and yes, MPW will color-code it in this way).

   #include <stdio.h>

   main()
   {
      printf("hello world\n");
   }

When you type in the program be sure to only use the 'return' key, don't use the 'enter' key yet.

Now switch back to the worksheet window. Technically speaking, you don't have to, but it's better than filling up your C program file with MPW commands.

In the worksheet window, try the command:

   mrc hello.c
Remeber to use the 'enter' key, not the 'return' key. That command should work if your file 'hello.c' is in the same folder as the MPW Shell application. Note that commands are not case-sensitive. You'll know if the command works because you'll get an error:
MrC C Compiler 3.0.1f1c12
Copyright (C) 1994-1997 by Apple Computer, Inc.

}
^
File "hello.c"; line 6 #Warning 30: implied return at closing '}' of main does not return value
#-----------------------
It's only a warning, though, and you should see that you've also got a file named 'hello.c.o' in the same folder as the MPW Shell application and your 'hello.c' file.

However, you can't 'run' hello.c.o and the reason has nothing to do with the warning error. To actually run the hello world program, we need to do more than compile it, we also need to 'build' it.

Check out MPW's 'Build' menu. The first item is 'Create Build Commands'. You'll need both the Commando and CreateMake tools for this dialog to function.

Start in the upper left of the dialog and for program name, type in 'hello' (without the quotes). The button on the upper right is labelled 'Source Files'. It will bring up a new dialog with a list of files. Find 'hello.c' and click the 'Add' button, then click 'Done' to make the extra dialog go away. Next, in the middle of the left side is a set of radio buttons for 'Program Type'. The 'Application' button is the default and you'd think that's what we want, but it isn't. All we've got is 'hello world'. That's far from a full-blown mac application with menus and windows and buttons and stuff like that. The easiest choice for now is 'MPW Tool'. I'll talk about one other option for this part later.

After that, you need to pick a 'Target'. Why they didn't use radio buttons here, I don't know. As soon as you pick one of the checkboxes, the other two become greyed out. I chose PowerPC only, as I have a PowerPC and am interested only in running my program on my machine. If you are unsure about whether your mac is a PowerPC or a 68K, or if you want to be able to share your program with ALL other mac users, FAT is the one to pick.

Once you've done those things, the 'CreateMake' button should become active. There are a couple of 'options' buttons that may or may not be active, depending on what Target you've chosen. Feel free to explore those, but you don't really have to worry about them now.

Clicking on the CreateMake button won't seem to do much in your MPW shell (unless you're missing something and it gives you errors). A file named 'hello.make' will appear in the folder along with hello.c and the hello.c.o file we made by compiling it earlier.

The next item down on the 'Build' menu is just plain 'Build'. There's a keyboard shortcut for it, propeller-B. That will pop up a dialog asking for the program name, and it should already say 'hello' in there, since that's the name of the program you just created the makefile for. Go ahead and build it.

You should see all kinds of cool stuff depending on what sorts of settings you had in your makefile. This is my output:


# 1:31:21 PM ----- Build of hello.
# 1:31:21 PM ----- Analyzing dependencies.
# 1:31:22 PM ----- Executing build commands.
    MrC hello.c -o hello.c.x
MrC C Compiler 3.0.1f1c12
Copyright (C) 1994-1997 by Apple Computer, Inc.

}
^
File "hello.c"; line 6 #Warning 30: implied return at closing '}' of main does not return value
#-----------------------
    PPCLink -o hello "hello.c.x" -t 'MPST' -c 'MPS ' "HD:MPW:Libraries:SharedLibraries:InterfaceLib" "HD:MPW:Libraries:SharedLibraries:StdCLib" "HD:MPW:Libraries:SharedLibraries:MathLib" "HD:MPW:Libraries:PPCLibraries:StdCRuntime.o" "HD:MPW:Libraries:PPCLibraries:PPCCRuntime.o" "HD:MPW:Libraries:PPCLibraries:PPCToolLibs.o"

# 1:31:24 PM ----- Done.
It also prints out the word 'hello' and leaves the input cursor on the same line. Use the 'enter' key to run your program!
# 1:31:24 PM ----- Done.
   hello  
hello world
The actual building of the program will have created two more files. One is named hello.c.x and the other is simply named 'hello'. If you do a 'Get Info' on this plain 'hello' file, it will show up as kind 'MPW Tool'. And just like any other MPW Tool, if your MPW Shell sees it, you can type the name of the command and the enter key to run it any time.

All that, just to run 'hello world'. If you want a little bit more, you can move on to...

Part 4 -- Hello world as a stand-alone mac application!

Remember back in Part 3 where I talked about the Program Type radio buttons on the CreateMake dialog? There's an option called 'SIOW App.' SIOW stands for Simple Input/Output Window and you'll want to use this when you get to writing programs that accept user input. The MPW Shell can run programs that use 'scanf' and other routines that accept user input, but it doesn't handle the I/O nearly as nicely as an SIOW app.

I've put this stuff in a seperate section because I must have been doing this at the wrong time. As of this writing, there's a problem in the file Interfaces:Rincludes:SIOW.r downloaded from Apple that makes building your program as an SIOW App barf horribly and not work. You'll know you have the bad one if you get errors from that file starting at line 137.

Thank heaven for 'DejaNews', as I found an article that explained the problem quite clearly and enabled me to fix my copy of SIOW.r. I think you'll be better off if you fix your own SIOW.r, it's really easy.

Once the build finishes, you'll again see your program name on a line with the input cursor, hit the 'enter' key here to launch your application, or go back to the mac finder and double-click your 'hello' application. It will open a new window that says 'hello world' in it. You then quit out of the app like any other mac app. It will ask you if you want to save -- this allows you to save the plain text in the window to a file. That's quite handy for when you need to turn in your program's output along with the code when you are doing your homework in that Introduction to C Programming class!

 

Part 5 -- Using MPW to program in Java
For the last few months, I've been studying Java with my mac and MPW. Here's the current info on using MPW to program in Java.

Download a bunch of stuff
Go to Apple's Java Developer's page (link opens in new window). The current releases as of this update are MRJ 2.1 for Macintosh Runtime Java (Feb 15 '99) and MRJ SDK 2.1 Macintosh Runtime Java Software Developer Kit (Mar 9 '99). You will need both the Java Runtime and the SDK. There are some other good links on that page, Java API documentation in mac format is one to look for.

Put copies in MPW's search path
In the SDK, you'll find a folder called 'Tools' under which will be another folder called 'MPW Tools'. The MPW Tools folder contains a text file 'About MPW Tools' which is worth a read, four MPW Document files; Jar, JavaC, JavaDoc and JavaH, and an MPW Tool called RunJava. I stashed copies of all but the text file under my 'MPW:User Commands' folder.

Using MPW to edit and compile Java code
When MPW is editing a file that ends in ".java" it will colorize your program text similarly to the way it does for C programs - comments in red, keywords in blue, most text in black, anything inside quotes grey - which I find to be very useful.

To compile the code, use the 'JavaC' tool in the same manner as the 'MrC' tool described above. MPW will report compile errors with line numbers and the MPW command "line 42" will take you to line 42 in your most recently edited open .java file. Sucessfully compiled code results in one or more .class files.

A word about CLASSPATH
For a long while I'd pack all my classes inside the same .java file, which goes against the standard practice of one class per .java file but was the only way I could have MPW see all my classes at compile time. Then I learned about the easy and mac-like way to "add a directory to your CLASSPATH" by putting an alias of any folders containing multiple .class files into the "HD:System Folder:Extensions:MRJ Libraries:MRJClasses" folder. Both .jar and .zip files can also be put here.

Problems running Java from inside MPW
I still haven't gotten RunJava to work, I keep getting "Error: Couldn't find java tool." If you know how I can fix this, please fill me in, thanks!

Use MRJ tools to run code
To run my Java programs, I've used either Apple's Applet Runner or the JBindery application that comes with the SDK, depending on whether I'd written an applet or an application, respectively. Drag and drop is the way to go with these.

  • To run applets, drop an .html file onto the Applet Runner icon.
  • To run applications, drop the .class file containing the main method onto the JBindery icon.

 

About Calyxa