The linker combines several object and library files into one output file. ld65 is very configurable, but fortunately has a built-in configuration for the C64, 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,
"c64.lib
". We have to specify that file on the command line, so that the
linker can resolve those functions.
A second file (this time, an object file) needed is the startup code that prepares the grounds for the C program to run. The startup file must be executed first, so it must be the first file on the linker command line.
Let's link our files to get the final executable:
ld65 -t c64 -o hello c64.o hello.o text.o c64.lib
The argument after -o
specifies the name of the output file, the argument
after -t
gives the target system. As discussed, the startup file must be
the first input file on the command line (you may have to add a path here, if
c64.o
is not in your current directory). Since the 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
", ready for
our C64!
For more information about the linker, see ld65.html.