1 <!doctype linuxdoc system>
5 <title>cc65 compiler intro
6 <author>Ullrich von Bassewitz, <htmlurl url="mailto:uz@cc65.org" name="uz@cc65.org">
10 How to use the cc65 C compiler - an introduction.
13 <!-- Table of contents -->
16 <!-- Begin the document -->
20 This is a short intro, how to use the compiler and the binutils. It contains a
21 step-by-step example, how to build a complete application from one C and one
22 assembler module. This file does <em/not/ contain a complete reference for the
23 tools used in the process. There are separate files describing these tools in
26 <bf>Note</bf>: There is a much simpler way to compile this example using the
27 cl65 compiler and link utility. However, it makes sense to understand how the
28 separate steps work. How to do the example with the cl65 utility is described
29 <ref id="using-cl65" name="below">.
32 <sect1>The sample modules<p>
34 To explain the development flow, I will use the following example modules:
42 extern const char text[]; /* In text.s */
46 printf ("%s\n", text);
54 _text: .asciiz "Hello world!"
58 <sect1>Translation phases<p>
60 We assume that the target file should be named "hello", and the target system
70 +---------+ +---------+
71 | hello.s | | text.s |
72 +---------+ +---------+
76 +---------+ +---------+ +----------+ +---------+
77 | hello.o | | text.o | | c64.o | | c64.lib |
78 +---------+ +---------+ +----------+ +---------+
82 +----------------------->ld65<-------------------------+
87 <tt/c64.o/ (the startup code) and <tt/c64.lib/ (the c64 version of the runtime
88 and C library) are provided in binary form in the cc65 package.
94 The compiler translates one C source into one assembler source for each
95 invocation. It does <em/not/ create object files directly, and it is <em/not/
96 able to translate more than one file per run.
98 In the example above, we would use the following command line, to translate
99 <tt/hello.c/ into <tt/hello.s/:
102 cc65 -O -I ../include -t c64 hello.c
105 The <tt/-O/ switch tells the compiler to do an additional optimizer run, which
106 is usually a good idea, since it makes the code smaller. If you don't care
107 about the size, but want to have slightly faster code, use <tt/-Oi/ to inline
108 some runtime functions.
110 The <tt/-I/ switch gives a search path for the include files. You may also set
111 the environment variable CC65_INC to the search path.
113 The <tt/-t/ switch is followed by the target system.
115 If the compiler does not complain about errors in our hello world, we will
116 have a file named "<tt/hello.s/" in our directory that contains the assembler
117 source for the hello module.
119 For more information about the compiler see <htmlurl url="cc65.html"
124 <sect>The assembler<p>
126 The assembler translates one assembler source into an object file for each
127 invocation. The assembler is <tt/not/ able to translate more than one source
130 Let's translate the hello.s and text.s files from our example:
137 The <tt/-t/ switch is needed when translating the <tt/text.s/ file, so the
138 text is converted from the input character set (usually ISO-8859-1) into the
139 target character set (PETSCII) by the assembler. The compiler generated file
140 <tt/hello.s/ does not contain any character constants, so specification of a
141 target is not necessary (it wouldn't do any harm, however).
143 If the assembler does not complain, we should now have two object files (named
144 <tt/hello.o/ and <tt/text.o/) in the current directory.
146 For more information about the assembler see <htmlurl url="ca65.html"
153 The linker combines several object and library file into one output file. ld65
154 is very configurable, but fortunately has a builtin configuration for the C64,
155 so we don't need to mess with configuration files here.
157 The compiler uses small functions to do things that cannot be done inline
158 without big impact on code size. These runtime functions, together with the C
159 library are in an object file archive named after the system, in this case
160 "<tt/c64.lib/". We have to specify this file on the command line so that the
161 linker can resolve these functions.
163 A second file (this time an object file) needed, is the startup code that
164 prepares the grounds for the C program to run. The startup file must be
165 executed first, so it must be the first file on the linker command line.
167 Let's link our files to get the final executable:
170 ld65 -t c64 -o hello c64.o hello.o text.o c64.lib
173 The argument after <tt/-o/ specifies the name of the output file, the argument
174 after <tt/-t/ gives the target system. As discussed, the startup file must be
175 the first file on the command line (you may have to add a path here, if
176 <tt/c64.o/ is not in your current directory). Since the library resolves
177 imports in <tt/hello.o/ and <tt/text.o/, it must be specified <em/after/ these
180 After a successful linker run, we have a file named "<tt/hello/", ready for
183 For more information about the linker see <htmlurl url="ld65.html"
188 <sect>The easy way (using the cl65 utility)<label id="using-cl65"><p>
190 The cl65 utility is able to do all of the steps described above in just one
191 call, and it has defaults for some options that are very well suited for our
194 To compile both files into one executable enter
197 cl65 -O -I ../include hello.c test.s
200 (The <tt/-I/ switch is not needed if you are working under Linux with the
201 include files in the default path, or the <tt/CC65_INC/ environment variable
204 The cl65 utility knows, how to translate C files into object files (it will
205 call the compiler and then the assembler). It does also know how to create
206 object files from assembler files (it will call the assember for that). It
207 knows how to build an executable (it will pass all object files to the
208 linker). And, finally, it has the C64 as a default target and will supply the
209 correct startup file and runtime library names to the linker, so you don't
210 have to care about that.
212 The one-liner above should give you a C64 executable named "<tt/hello/" in the
215 For more information about the compile & link utility see <htmlurl
216 url="cl65.html" name="cl65.html">.