cc65 Compiler Intro
1. Overview
This is a short intro of how to use the compiler and the bin-utils. It contains a step-by-step example of how to build a complete application from one C and one assembly modules. This file does not contain a complete reference for the tools used in the process. There are separate files describing those tools, in detail (see index.html).
I do assume that you have built or installed the compiler and the Atari Lynx
target files (the only target supported by this tree). If the tools cannot
find the include files, libraries or linker configs, set the environment
variables CC65_INC, LD65_LIB and LD65_CFG as described below.
Note: There is a much simpler way to compile this example, by using the cl65 compile-and-link utility. However, it makes sense to understand how the separate steps work. How to do the example with the cl65 utility is described later.
1.1. Before we start
Create the two small sample modules from the next section in a working
directory (a complete Lynx sample is also available in the
"examples" directory of the source tree). If you encounter problems
with missing include files and/or libraries, please check the environment variables
CC65_INC, LD65_LIB and LD65_CFG. They should point to the
include, lib and cfg subdirectories of the directory, where you
installed cc65.
1.2. The sample modules
To explain the development flow, I will use the following example modules:
hello.c:
#include <string.h>
extern const char text[]; /* In text.s */
int main (void)
{
/* The Lynx has no text console, so there is nothing to print
here; this module only demonstrates the build flow. For
on-screen text, see the graphical Hello World in section 6 below. */
return (int) strlen (text);
}
text.s:
.export _text
_text: .asciiz "Hello world!"
1.3. Translation phases
We assume that the target file should be named "hello", and the target system is the Atari Lynx.
+---------+
| hello.c |
+---------+
|
cc65
\/
+---------+ +---------+ +----------+
| hello.s | | text.s | | crt0.o |
+---------+ +---------+ +----------+
| | |
ca65 ca65 ar65
\/ \/ \/
+---------+ +---------+ +----------+
| hello.o | | text.o | | lynx.lib |
+---------+ +---------+ +----------+
| \ /
| \ /
| \ /
+----------------------->ld65<
\/
hello
crt0.o (the startup code) and lynx.lib (the always-linked
core library: runtime, startup glue, base platform and C library) are
built from this tree. Actually, the startup code is contained in the library, so
you won't need to care about it. The SDK also builds optional subsystem
libraries — lynx-graphics.lib, lynx-audio.lib,
lynx-math.lib and lynx-compress.lib — that a
program opts into only when it uses those subsystems (see below).
2. The compiler
The compiler translates one C source into one assembly source, for each invocation. It does not create object files directly, and it is not able to translate more than one file per run.
In the example above, we would use the following command line, to translate
hello.c into hello.s:
cc65 -O hello.c
The -O switch tells the compiler to do an additional optimizer run, which
is usually a good idea, since it makes the code smaller. If you don't care
about the size, but want to have slightly faster code, use -Oi to inline
some runtime functions.
The optional -t switch is followed by the target system name. It is
accepted only for backwards compatibility — the only valid value is
lynx — and has no effect, because this toolchain always targets
the Atari Lynx.
If the compiler does not complain about errors in our "hello world" program, we
will have a file named "hello.s", in our directory, that contains the
assembly source for the hello module.
For more information about the compiler, see cc65.html.
3. The assembler
The assembler translates one assembly source into an object file, for each invocation. The assembler is not able to translate more than one source file per run.
Let's translate the "hello.s" and "text.s" files from our example:
ca65 hello.s
ca65 text.s
When translating the text.s file, the assembler converts the text from
the input character-set (usually ISO-8859-1) into the target character-set
(ASCII, in this example). This happens automatically, because the assembler
always uses the Atari Lynx character map. The optional -t lynx switch is
still accepted for backwards compatibility, but it is not needed and has no
effect.
If the assembler does not complain, we should now have two object files (named
hello.o and text.o) in the current directory.
For more information about the assembler, see ca65.html.
4. The linker
The linker combines several object and library files into one output file. ld65 is very configurable, but fortunately has built-in configurations, so we don't need to mess with configuration files, here.
The compiler uses small functions to do things that cannot be done inline
without a big impact on code size. Those runtime functions, together with the
C library, are in an object-file archive named after the system, in this case,
"lynx.lib". We have to specify that file on the command line, so that the
linker can resolve those functions.
Code that uses an optional subsystem — the Lynx graphics, Mikey audio,
Suzy hardware math, or the compression routines — also names the matching
archive (lynx-graphics.lib, lynx-audio.lib,
lynx-math.lib, lynx-compress.lib). List the optional
libraries before the core lynx.lib, so a single in-order
linker pass resolves every cross-reference (the linker pulls in only the modules
a program actually references, so naming a subsystem it doesn't use costs
nothing). For example, a program that draws with Lynx graphics and uses Suzy math:
ld65 -o hello hello.o text.o lynx-graphics.lib lynx-math.lib lynx.lib
Let's link our files to get the final executable:
ld65 -o hello hello.o text.o lynx.lib
The argument after -o specifies the name of the output file. The
following arguments are object files or libraries. (An optional -t lynx
switch is accepted for backwards compatibility but has no effect; the linker
always loads the Lynx configuration by default.) Since the target library
resolves imports in hello.o
and text.o, it must be specified after those files.
After a successful linker run, we have a file named "hello", a bootable
Lynx cart image!
For more information about the linker, see ld65.html.
5. The easy way (using the cl65 utility)
The cl65 utility is able to do all of the steps described above, in just one command line, and it has defaults for some options that are very well-suited for our example.
To compile both files into one executable, enter:
cl65 -O hello.c text.s
The cl65 utility knows how to translate C files into object files (it will call the compiler, and then the assembler). It does know also how to create object files from assembly files (it will call only the assembler, for that). It knows how to build an executable (it will pass all object files to the linker). And finally, it has the Lynx as its default target, and will supply the correct startup file and runtime library names to the linker, so you don't have to care about that.
It also resolves the optional SDK subsystem libraries automatically. Unlike
the explicit ld65 invocation above — where you list
lynx-graphics.lib, lynx-math.lib and the rest yourself
— cl65 appends the whole SDK set for you (in the correct dependents-first,
core-last order), so a program that draws with Lynx graphics or uses Suzy math links
with no -l flags at all. The linker still pulls in only
the modules a program actually references, so unused subsystems cost nothing.
Pass --no-sdk-libs if you would rather name the optional libraries
explicitly. See cl65.html for details.
The one-liner above should give you a Lynx cart image named "hello" in
the current directory.
For more information about the compile & link utility, see cl65.html.
To start a real project rather than this tutorial, copy the
templates/basic directory out of the SDK: it is a minimal, complete
game (a Makefile that uses the automatic SDK libraries plus a
src/main.c that brings up Lynx graphics and reads the joystick) and is the
canonical example of the <lynx/...> include layout and the
zero-config link path. See its README.md for details.
6. A Hello World you can see
The two-module example above exercises the whole toolchain, but it draws
nothing: the Lynx has no text console, so main can only compute a
value and return it. Here is a self-contained program that actually puts text on
the Lynx screen, using the SDK's Lynx graphics library:
#include <lynx/lynx.h>
#include <lynx/gfx.h>
#include <6502.h>
void main(void) {
gfx_init();
CLI();
while (gfx_busy())
;
gfx_clear();
gfx_setcolor(GFX_COLOR_GREEN);
gfx_outtextxy(0, 0, "Hello World");
gfx_updatedisplay();
while (1)
;
}
<lynx/lynx.h> brings in the Lynx system definitions and the
color constants, <lynx/gfx.h> declares the graphics functions,
and <6502.h> supplies the CLI() wrapper that
enables interrupts once the graphics system is initialised.
Because the Lynx has no ASCII text hardware, on-screen characters are drawn by
the Lynx graphics library rather than printed. Unlike the upstream cc65 driver
model, this SDK's Lynx graphics are a static, direct-call
library: there is no relocatable driver to load at run time and nothing to
install — you simply call the gfx_* functions and link the
graphics library. See the function reference for the
full graphics API, and the Memory page for how the runtime
and screen buffers are laid out.
Build it with cl65, which supplies the startup code and links the SDK subsystem libraries (including Lynx graphics) automatically:
cl65 -O -o hello.lnx hello.c
The result is a bootable cart image, hello.lnx. With an explicit
ld65 link you would name lynx-graphics.lib before the
core lynx.lib, exactly as shown in section 4.
7. Running The Executable
The Lynx has no operating system or text screen: the linker output is a
bootable cart image, and text output requires the Lynx graphics library.
The program in the tutorial above therefore produces no visible
output on the Lynx - it demonstrates the build mechanics only. For a
program that actually displays text on the Lynx screen, see the graphical
Hello World in section 6 above. By convention, give the output file a
.lnx extension:
cl65 -O -o hello.lnx hello.c text.s
7.1. Emulators
- GearLynx
-
A very accurate, cross-platform Lynx emulator written in C++ for Windows, macOS, Linux, BSD and RetroArch. It includes an embedded MCP server for debugging and tooling, which makes it a strong choice for inspecting the code the compiler generates. Available at https://github.com/drhelius/Gearlynx.
- Holani
-
A newer Lynx emulator written in Rust that runs on Linux, macOS and Windows. A GTK frontend and a LibRetro core are also available. Available at https://github.com/LLeny/holani.
- Handy
-
The classic Atari Lynx emulator that most later emulators are based on, and the simplest way to get cart images running. Open the
.lnxfile from the File menu. Available at http://handy.sourceforge.net/. - Felix
-
A newer emulator project with enhanced features such as instruction tracing. It is in early development and currently runs on Windows only. Available at https://github.com/laoo/Felix.
- Mednafen
-
Available at https://mednafen.github.io/: a multi-system emulator with an accurate, cycle-counting Lynx core and a built-in debugger - useful for profiling the code the compiler generates. Run the cart image with:
mednafen hello.lnx
On real hardware, the cart image can be run from a flash/SD cartridge, or
uploaded over ComLynx using a BLL-compatible loader (link with
-C lynx-bll.cfg to produce a BLL download-compatible binary).