1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
5
<meta http-equiv="Content-Language" content="en-us">
6
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7
<title>Makefile</title>
8
<link rel="stylesheet" type="text/css" href="../help.css">
12
<h1 role="main">Makefile</h1>
13
<p>A makefile is a text file that is referenced by the make command that describes the building of targets, and contains information such as source-level dependencies and build-order dependencies. </p>
14
<p>The CDT can generate a makefile for you, such projects are called Managed Make projects. Some projects, known as Standard Make projects, allow you to define your own makefile.</p>
16
<h2>Sample Makefile</h2>
19
# This Makefile demonstrates and explains
20
# Make Macros, Macro Expansions,
21
# Rules, Targets, Dependencies, Commands, Goals
22
# Artificial Targets, Pattern Rule, Dependency Rule.
24
# Comments start with a # and go to the end of the line.
26
# Here is a simple Make Macro.
27
LINK_TARGET = test_me.exe
29
# Here is a Make Macro that uses the backslash to extend to multiple lines.
30
# This allows quick modification of more object files.
36
# Here is a Make Macro defined by two Macro Expansions.
37
# A Macro Expansion may be treated as a textual replacement of the Make Macro.
38
# Macro Expansions are introduced with $ and enclosed in (parentheses).
39
REBUILDABLES = $(OBJS) $(LINK_TARGET)
41
# Make Macros do not need to be defined before their Macro Expansions,
42
# but they normally should be defined before they appear in any Rules.
43
# Consequently Make Macros often appear first in a Makefile.
45
# Here is a simple Rule (used for "cleaning" your build environment).
46
# It has a Target named "clean" (left of the colon ":" on the first line),
47
# no Dependencies (right of the colon),
48
# and two Commands (indented by tabs on the lines that follow).
49
# The space before the colon is not required but added here for clarity.
54
# There are two standard Targets your Makefile should probably have:
55
# "all" and "clean", because they are often command-line Goals.
56
# Also, these are both typically Artificial Targets, because they don't typically
57
# correspond to real files named "all" or "clean".
59
# The rule for "all" is used to incrementally build your system.
60
# It does this by expressing a dependency on the results of that system,
61
# which in turn have their own rules and dependencies.
65
# There is no required order to the list of rules as they appear in the Makefile.
66
# Make will build its own dependency tree and only execute each rule only once
67
# its dependencies' rules have been executed successfully.
69
# Here is a Rule that uses some built-in Make Macros in its command:
70
# $@ expands to the rule's target, in this case "test_me.exe".
71
# $^ expands to the rule's dependencies, in this case the three files
72
# main.o, test1.o, and test2.o.
73
$(LINK_TARGET) : $(OBJS)
76
# Here is a Pattern Rule, often used for compile-line.
77
# It says how to create a file with a .o suffix, given a file with a .cpp suffix.
78
# The rule's command uses some built-in Make Macros:
79
# $@ for the pattern-matched target
80
# $lt; for the pattern-matched dependency
84
# These are Dependency Rules, which are rules without any command.
85
# Dependency Rules indicate that if any file to the right of the colon changes,
86
# the target to the left of the colon should be considered out-of-date.
87
# The commands for making an out-of-date target up-to-date may be found elsewhere
88
# (in this case, by the Pattern Rule above).
89
# Dependency Rules are often used to capture header file dependencies.
90
Main.o : Main.h Test1.h Test2.h
91
Test1.o : Test1.h Test2.h
94
# Alternatively to manually capturing dependencies, several automated
95
# dependency generators exist. Here is one possibility (commented out)...
97
# g++ -M $(FLAGS) $< > $@
98
# include $(OBJS:.o=.dep)
102
<h2>Frequently Asked Questions:</h2>
103
Your Console view can be very useful for debugging a build.
105
<p><strong>Q1. My Console view says <span class="typewriter"><Q>Error launching builder</Q></span>. What does that mean?</strong></p>
107
Error launching builder (make -k clean all )
108
(Exec error:Launching failed)
111
<p>Most probably, the build command (by default "make") is not on your path. You can put it on your path and restart Eclipse.<br>
112
You can also change the build command to something that is on your path. If you are using MinGW tools to compile, you should replace the build command with "mingw32-make".</p>
114
<p><strong>Q2. My Console view says <span class="typewriter"><Q>No rule to make target 'X'</Q></span>.</strong></p>
117
make: *** No rule to make target 'clean'.
118
make: *** No rule to make target 'all'.
121
<p>By default, the make program looks for a file most commonly called "Makefile" or "makefile".
122
If it cannot find such a file in the working directory, or if that file is empty or the file does not
123
contain rules for the command line goals ("clean" and "all" in this case), it will normally fail
124
with an error message similar to those shown. </p>
126
<p>If you already have a valid Makefile, you may need to change the working directory of your build. The default working directory for the build command is the project's root directory. You can change this by specifying an alternate Build Directory in the Make Project properties.
127
Or, if your Makefile is named something else (eg. <span class="typewriter">buildFile.mk</span>), you can specify the name by setting the default Build command to <span class="typewriter">make -f buildFile.mk</span>.</p>
129
<p>If you do not have a valid Makefile, create a new file named Makefile in the root directory. You can then add the contents of the sample Makefile (above), and modify it as appropriate.</p>
131
<p><strong>Q3. My Console view says <span class="typewriter">"missing separator"</span>.</strong></p>
134
makefile:12: *** missing separator. Stop.
136
<p>The standard syntax of Makefiles dictates that every line in a build rule must be preceded by a Tab character.
137
This Tab character is often accidentally replaced with spaces, and because both result in white-space indentation,
138
this problem is easily overlooked. In the sample provided, the error message can be pinpointed to line 12 of the
139
file "makefile"; to fix the problem, insert a tab at the beginning of that line.</p>
141
<p><strong>Q4. My Console view says <span class="typewriter"><Q>Target 'all' not remade because of errors</Q></span>.</strong></p>
144
make: *** [clean] Error 255
145
rm -f Test1.o Test2.o Main.o test_me.exe
146
g++ -g -o Test1.o -c Test1.cpp
147
make: *** [Test1.o] Error 255
148
make: *** [Test2.o] Error 255
149
make: *** [Main.o] Error 255
150
g++ -g -o Test2.o -c Test2.cpp
151
g++ -g -o Main.o -c Main.cpp
152
make: Target 'all' not remade because of errors.
154
<p>The likely culprit here is that g++ is not on your Path.<br>
155
<p>The Error 255 is produced by make as a result of its command shell not being able to find a command for a particular rule.<br>
156
Messages from the standard error stream (the lines saying Error 255) and standard output stream (all the other lines) are merged in the Console view here.</p>
158
<p><strong>Q5. What's with the -k flag?</strong></p>
160
<p>The -k flag tells make to continue making other independent rules even when one rule fails.
161
This is helpful for build large projects.</p>
162
<p>You can remove the -k flag by turning on Project Properties > C/C++ Make Project > Make Builder > Stop on first build error</p>
164
<p><strong>Q6. My Console view looks like:</strong></p>
166
mingw32-make clean all
167
process_begin: CreateProcess((null), rm -f Test1.o Test2.o Main.o test_me.exe, ...) failed.
168
make (e=2): The system cannot find the file specified.
170
mingw32-make: *** [clean] Error 2
171
rm -f Test1.o Test2.o Main.o test_me.exe
174
<p>This means that mingw32-make was unable to find the utility "rm". Unfortunately, MinGW does not come with "rm". To correct this, replace the clean rule in your Makefile with:</p>
180
<p>The leading minus sign tells make to consider the clean rule to be successful even if the del command returns failure. This may be acceptable since the del command will fail if the specified files to be deleted do not exist yet (or anymore).</p>
183
<p><img src="../images/ng00_04a.gif" ALT="IBM Copyright Statement" ></p>
b'\\ No newline at end of file'