carbidecpp22devenv/configuration/org.eclipse.osgi/bundles/309/1/.cp/concepts/cdt_c_makefile.htm
changeset 5 684bf18fdedf
equal deleted inserted replaced
4:4764c8c88759 5:684bf18fdedf
       
     1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
       
     2 <html lang="en">
       
     3 
       
     4 <head>
       
     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">
       
     9 </head>
       
    10 
       
    11 <body>
       
    12 <h1>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>
       
    15 
       
    16 <h2>Sample Makefile</h2>
       
    17 <pre>
       
    18 # A sample Makefile
       
    19 # This Makefile demonstrates and explains 
       
    20 # Make Macros, Macro Expansions,
       
    21 # Rules, Targets, Dependencies, Commands, Goals
       
    22 # Artificial Targets, Pattern Rule, Dependency Rule.
       
    23 
       
    24 # Comments start with a # and go to the end of the line.
       
    25 
       
    26 # Here is a simple Make Macro.
       
    27 LINK_TARGET = test_me.exe
       
    28 
       
    29 # Here is a Make Macro that uses the backslash to extend to multiple lines.
       
    30 # This allows quick modification of more object files.
       
    31 OBJS =  \
       
    32  Test1.o \
       
    33  Test2.o \
       
    34  Main.o
       
    35 
       
    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)
       
    40 
       
    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.
       
    44 
       
    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.
       
    50 clean : 
       
    51  rm -f $(REBUILDABLES)
       
    52  echo Clean done
       
    53 
       
    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".  
       
    58 
       
    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.
       
    62 all : $(LINK_TARGET)
       
    63  echo All done
       
    64 
       
    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.
       
    68 
       
    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)
       
    74  g++ -g -o $@ $^
       
    75 
       
    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
       
    81 %.o : %.cpp
       
    82  g++ -g -o $@ -c $&lt;
       
    83 
       
    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
       
    92 Test2.o : Test2.h
       
    93 
       
    94 # Alternatively to manually capturing dependencies, several automated
       
    95 # dependency generators exist.  Here is one possibility (commented out)...
       
    96 # %.dep : %.cpp
       
    97 #        g++ -M $(FLAGS) $&lt; &gt; $@
       
    98 # include $(OBJS:.o=.dep)
       
    99 </pre>
       
   100 
       
   101 
       
   102 <h2>Frequently Asked Questions:</h2>
       
   103 Your Console view can be very useful for debugging a build.
       
   104 
       
   105 <p><font size="+1"><b>Q1</b>.  My Console view says <tt>"Error launching builder"</tt>. What does that mean?<p></font>
       
   106 <pre>
       
   107 Error launching builder (make -k clean all )
       
   108 (Exec error:Launching failed)
       
   109 </pre>
       
   110 
       
   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>
       
   113 
       
   114 <p><font size="+1"><b>Q2</b>. My Console view says <tt>"No rule to make target 'X'"</tt>.</p></font>
       
   115 <pre>
       
   116 make -k clean all 
       
   117 make: *** No rule to make target 'clean'.
       
   118 make: *** No rule to make target 'all'.
       
   119 </pre>
       
   120 
       
   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>
       
   125 
       
   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. <tt>buildFile.mk</tt>), you can specify the name by setting the default Build command to <tt>make -f  buildFile.mk</tt>.</p>
       
   128 
       
   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>
       
   130 
       
   131 <p><font size="+1"><b>Q3</b>. My Console view says <tt>"missing separator"</tt>.</p></font>
       
   132 <pre>
       
   133 make -k clean all 
       
   134 makefile:12: *** missing separator.  Stop.
       
   135 </pre>
       
   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>
       
   140 
       
   141 <p><font size="+1"><b>Q4</b>. My Console view says <tt>"Target 'all' not remade because of errors"</tt>.</p></font>
       
   142 <pre>
       
   143 make -k clean all 
       
   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.
       
   153 </pre>
       
   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>
       
   157 
       
   158 <p><font size="+1"><b>Q5</b>. What's with the -k flag?</p></font>
       
   159 
       
   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>
       
   163 
       
   164 <p><font size="+1"><b>Q6</b>. My Console view looks like:</p></font>
       
   165 <pre>
       
   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.
       
   169 
       
   170 mingw32-make: *** [clean] Error 2
       
   171 rm -f Test1.o Test2.o Main.o test_me.exe
       
   172 </pre>
       
   173 
       
   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>
       
   175 <p><pre>
       
   176 clean : 
       
   177 	-del $(REBUILDABLES)
       
   178 	echo Clean done
       
   179 </pre></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>
       
   181 
       
   182 
       
   183 <p><img src="../images/ng00_04a.gif" ALT="IBM Copyright Statement" ></p>
       
   184 
       
   185 </body>
       
   186 </html>