C/C++ Programming for 68k

From 68kMLA Wiki
Jump to: navigation, search

Getting Started

This guide assumes a few things:

  • You already have a way of getting files from the Internet onto your Mac.
  • You have a Mac running at least System 7
  • You have some basic knowledge of C/C++. (This isn't a C/C++ guide but rather a Mac-specific guide)


Once you've met these requirements, the first step is to download the Macintosh Programmer's Workshop (MPW) from Apple's website. This is the official Apple-created IDE for Macintosh development. It can create apps that run on 68k, PPC, and even OS X machines. It can be downloaded from Apple's official website here.


Installing MPW is simple. Download the file MPW-GM.img.hqx and mount the disk image. Drag the MPW-GM folder wherever you like on your Mac's hard drive. Inside are two folders, "Interfaces&Libraries" and "MPW". The Interfaces&Libraries folder contains header files for different languages supported by MPW, including C/C++. It can be useful to refer to these header files at times because documentation for the Mac Toolkit can be hard to come by. The C files are in the folder CIncludes. For now though, open the MPW folder and run the application "MPW Shell". This is the main MPW interface.


You should see a screen with lots of red text and a few help commands in blue. This is a MPW "worksheet" which is sort of a cross between a document and a command line. Although you can edit and save documents like a normal text editor, you can also execute commands by hitting the Enter key on the numpad. You can test this by going to the bottom of the document and typing "Help Commands" then hitting Enter (on the numpad, Return won't work). MPW is similar to a Unix shell in a lot of ways. If you have experience with command lines, this can help a lot. The MPW has its own scripting language that is very powerful and useful, but can be difficult to learn. Fortunately, most of the features can also be accessed through menu interfaces.


Before we can create a new program, we need a place to keep the files that we're working on. MPW generates a lot of files and keeping them organized is helpful. First, go to the Finder and create a folder somewhere to store your code files. Then, go back to MPW and choose File->New... and create a file in this folder called "helloworld.cpp" (or whatever you like). Enter the following code into your new file:


#include <stdio.h>

void main () {
    printf("Hello World");
}


Then save the file. Now, you need to set the current working directory to wherever you saved the file. Click Directory->Set Directory and choose the folder that you saved the file in. Now all output from your program will be stored in this folder.


To compile this file, you need to create a Makefile, which is a set of instructions that tells MPW how to turn the code into a program. Click "Build"->"Create Build Commands..." Enter a name for your program and choose SIOW App." for the program type. SIOW stands for "Simple Input/Output Window" and is an interface that MPW provides for learning Mac OS programming. For more serious programming, you would choose to compile as a regular application. For Target, select the type of CPU your machine has, or "Fat Binary" if you're not sure. For PowerPC Macs, Classic refers to System 7 - Mac OS 9, while Carbon apps run on Mac OS 8 - X. Carbon apps also use different libraries, which means the programs themselves will have to use different code. For 68k, Near compiles the app as a 16-bit application, while Far compiles it as a 32-bit app (which might cause problems on really old Macs). Fat Binaries contain both 68k and PPC code.


Once you've got your compilation options configured, choose "Source Files..." and add the helloworld.cpp file. Then click "Done" and click "CreateMake" to finish the build command. Now choose Build->Build and enter the name of your app. The program will start to compile and any messages generated will be printed to the main MPW worksheet. The application will be placed in the working directory (which should be the same place as your code files). Running it should produce a window that says "Hello World".


Next: Macintosh Toolkit Programming