1 <!doctype linuxdoc system>
5 <title>Using GNU Make with cc65
6 <author>Oliver Schmidt, <htmlurl url="mailto:ol.sc@web.de" name="ol.sc@web.de">
10 How to build your program using the GNU Make utility.
13 <!-- Table of contents -->
16 <!-- Begin the document -->
20 This document describes how to build your programs using the cc65 development
21 tools and the GNU Make utility.
23 The cc65 development package doesn't come with a make utility. However this is
24 no issue because GNU Make works very nicely with cc65.
28 <sect>What is GNU Make?<p>
30 GNU Make is a both very powerful and very popular make utility. It might even
31 be called the de facto standard for make utilities. For more information see
32 the GNU Make home page:
34 <url url="http://www.gnu.org/software/make/">
36 The cc65 development package is available as binaries for several host systems
37 and can easily built for quite some additional systems. The very same is true
38 for GNU Make so a cc65-based project coming with a GNU Make Makefile can easily
39 be built by any cc65 developer no matter what host system is used.
41 Because of the strong alignment of the cc65 compiler with the ISO C standard it
42 is very well feasible to compile a single C code base both with the cc65
43 compiler and other C compilers like for example GCC. GNU Make turns out to be
44 very well suited to build projects for several target systems using multiple
45 compilers as it isn't tied to any C compiler.
49 <sect>A sample Makefile<p>
51 This Makefile is a fully functional sample for compiling several C sources
52 (here <tt/foo.c/ and <tt/bar.c/) and link the resulting object files into an
53 executable program (here <tt/foobar/):
61 CC = $(CC65_HOME)/bin/cl65
62 CFLAGS = -t $(CC65_TARGET) --create-dep -O
63 LDFLAGS = -t $(CC65_TARGET) -m $(PROGRAM).map
67 LDFLAGS = -Wl,-Map,$(PROGRAM).map
70 ########################################
74 sed -e"s!.s:!.o:!p" -e"s![^\t]*\t\(.*\)!\1:!" < $(<:.c=.u) > $(@:.o=.d)
83 ifneq ($(MAKECMDGOALS),clean)
84 -include $(SOURCES:.c=.d)
88 rm -f $(SOURCES:.c=.o) $(SOURCES:.c=.d) $(PROGRAM) $(PROGRAM).map
91 $(CC) -c $(CFLAGS) $< -o $@
94 $(PROGRAM): $(SOURCES:.c=.o)
95 $(CC) $(LDFLAGS) $^ -o $@
99 <sect1>Invoking the sample Makefile<p>
101 Without any specific configuration the sample Makefile will compile and link
102 using GCC. In order to rather use cc65 the variable <tt/CC65_TARGET/ needs to be
103 defined. This may by done as an environment variable or simply as part of the
104 Makefile. However to quickly switch between compilers and/or cc65 targets it is
105 best done on the GNU Make command line like this:
111 The sample Makefile doesn't require cc65 to be "installed" in any way. Rather it
112 only presumes the single variable <tt/CC65_HOME/ to point to the directory the
113 cc65 packages were unpacked into. Again there are several ways to define this
114 variable but as its value typically won't change often it is best done as an
115 environment variable.
118 <sect1>Understanding the sample Makefile<p>
120 Most parts of the sample Makefile follow the guidelines in the
121 <htmlurl url="http://www.gnu.org/software/make/manual/make.html" name="GNU Make Manual">
122 that can be searched online for background information. The automatic generation of
123 dependency however rather works as described by the GNU Make maintainer Paul D. Smith in
124 <htmlurl url="http://make.paulandlesley.org/autodep.html#advanced" name="Advanced Auto-Dependencies">.
126 In the meantime GCC supports this method directly with the preprocessor option
127 <tt/-MP/ while cc65 requires some post-processing of the dependency file with
128 <tt/sed/ adding a second line like in this example:
131 foo.o: foo.c foo.h bar.h
136 <sect1>Invoking the sample Makefile on Windows<p>
138 The recommended way to use GNU Make on Windows is to install it as part of a
139 Cygwin environment. For more information see the Cygwin home page:
141 <url url="http://www.cygwin.com/">
143 If however installing Cygwin shouldn't be an option for one or the other reason
144 then the sample Makefile may be invoked from the Windows Command Prompt (cmd.exe)
145 by downloading the following programs:
148 <item>make.exe: <url url="http://gnuwin32.sourceforge.net/packages/make.htm">
149 <item>sed.exe: <url url="http://gnuwin32.sourceforge.net/packages/sed.htm">
150 <item>rm.exe: <url url="http://gnuwin32.sourceforge.net/packages/coreutils.htm">
155 <sect>Target-specific Variable Values<p>
157 The very limited resources of the cc65 target machines now and then require
158 manual optimization of the build process by compiling individual source files
159 with different compiler options. GNU Make offers
160 <htmlurl url="http://www.gnu.org/software/make/manual/html_node/Target_002dspecific.html" name="Target-specific Variable Values">
161 perfectly suited for doing so. For example placing the code of the two modules
162 <tt/foo/ and <tt/bar/ in the segment <tt/FOOBAR/ can be archived with this
163 target-specific variable definition:
166 foo.o bar.o: CFLAGS += --code-name FOOBAR