This is the final part of a five-part series of posts on compiling Amiga-specific C code on an (emulated) Amiga A1200. We’ll start with a machine with an empty hard drive, and over the series, build up to compiling an Amiga-specific program which uses the graphics library.
It’s time for some proper Amiga coding! We’ll take some example code from the Amiga Intuition Reference Manual, published by Commodore in 1986, we’ll get it to compile with VBCC. We’ll finish by talking about different compiler options that you may need if you want to go on and make modifications to the program, or write your own killer application.
The example code
Here it is. It’s about as straight-forward as it can be. The program creates a screen, opens up a window on that screen, and prints a “Hello Amiga” message in the new Window. It then listens for the user pressing the close button, and shuts down everything cleanly.
I had to make a few modifications from the original as-printed version … it declared some unused variables, and didn’t cleanly close its libraries. Anyway, here it is …
You could type this into
edit, but that’s going to be really hard work. I’d recommend using a text editor on your host system, and saving the file into the Shared folder that we’ve been using to share files between the host and our Amiga VM. I saved it as
Compiling the program
The naive approach would be to use something similar to the command we used last time to compile the hello world program:
vc -o hello_amiga hello_amiga.c
That’s going to produce a bunch of linker errors. Our file compiles just fine, but it makes use of externally declared symbols, like
OpenScreen. Well, actually it’s
_OpenScreen … I guess the header files have inline code which adds an underscore and calls an externally defined function of that name. Then the linker comes to tie everything together and produce our executable, it can’t resolve the definition of that routine, so we get linker errors.
vbcc ships with a
amiga.lib file which contains the implementations of all of the Amiga-specific functions. When we’re compiling a program, we have to tell
vc that we want to link with that library by adding
-lamiga to our command line.
vc -lamiga -o hello_amiga hello_amiga.c
This compiles, and running our executable with
hello_amiga works as expected …
OK, I didn’t say it was pretty.
Automatic library opening
We can remove some of the boiler plate that opens and closes the libraries we need (“intuition.library” and “graphics.library”), as vbcc will handle automatically opening and closing them for us.
Remove the definitions of
GfxBase (lines 10 and 11) and all the lines that refer to them.
With the same invocation of
vc as above, we get a load more linker errors, complaining that
_IntuitionBase are not defined. We need to link with another magic library that ships with
vbcc, this one is
auto.lib. To link against it, we add
-lauto to the command line …
vc -lamiga -lauto -o hello_amiga hello_amiga.c
Floating point math
At some point, you’re going to want to use some floating point math. E.g. double types and all that good stuff. Well, it doesn’t “just work”. You have to link with yet another vbcc provided library, this time you need to specify:
Alternatively, if you don’t need 68000 support, and you can target just 68020 processors or better (i.e. Amiga 1200 or later), you can instead make use of the FPU (floating point unit) with these commands:
It turns out that vbcc by default supports only a really old version of C. For example, you can’t even use end of line comments prefixed with
//. You can get C-99 support by adding the following switch to the command-line:
So, that’s about it. We’ve gone from a pristeen Amiga, installed everything we need to do C development, and got some Amiga-specific code to compile and run.
Where you go from here is up to you. I’d highly recommend getting some of the official Amiga reference manuals. There are copies available online, but I’m not sure how legitimate that is from a copyright perspective. Some that you could check out are the “Amiga Intuition Reference Manual”, the “Amiga Hardware Reference Manual”, “Amiga ROM Kernel: Devices” and the “Amiga ROM Kernel Reference Manual: Libraries and Devices”.