[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] / [CSci202] / make
[Text Version] [Syllabus] [Schedule] [Glossary] [Resources] [Grading] [Contact] [Question] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Thu Aug 25 21:09:38 PDT 2011



      Most projects have many different files, libraries, and programs. The UNIX 'make' program helps you rebuild the program when some of the files are changed. It calculates precisely the smallest number operations needed to reconstruct a new version and carries them out in the correct order.

      The 'make' program is normally controlled by a file called 'Makefile'. Think of a Makefile as a computerized cook-book:

         		popcorn: raw_popcorn
         			put raw_popcorn in microwave
         			push popcorn button and wait
         			push open button and remove popcorn
         			wipe inside of microwave
         			put empty bag in trash
         		snack: popcorn
         			eat popcorn

      The command
       		make popcorn
      will check to see that there is some raw_popcorn and if so puts it in the oven and pushes the button for us.

      If we try the command

       		make snack
      the make program first looks to see if we have popcorn, and if needed makes it for us... then we eat the popcorn. Notice that the recipes can be in any order and the make program selects the right ones to follow.


      A makefile is made of Recipes and Actions:


       	thing_to_be_made : raw_materials
       	<TAB>	step to make the thing from the raw materials
       	<TAB>	step to make the thing from the raw materials
      The thing_to_be_made is called the target of the rule(recipe) and the raw_materials are called the input.
       	<TAB>	step to do it
       	<TAB>	step to do it
      Notice the <TAB> character. This must be the real Tab character. Spaces do not work.

      Example 1

      Suppose we have a source code file called "memo.cpp" file that must be compiled into an executable memo. Here is a complete Makefile for this:
         memo : memo.cpp
         	g++ -w -O -o memo memo.cpp

      The "-w -O" requests that compilation gives few warning messages and try to optimize the program.

      Example 2

      Suppose are memo program uses a function called remind and that the code for this is a file called remind.cpp which is included in memo.cpp:
         memo : memo.cpp reminder.cpp
         	g++ -w -O -o memo memo.cpp

      Example 3

      However reminder.cpp is a large file and takes a long time to compile. Recompiling it each time is wasteful so we create a header file reminder.h and a separate "reminder.cpp" to define the bodies of functions. This will be precompiled when it change, and not otherwise. We need two recipes, like this:
         reminder.o : reminder.cpp reminder.h
         	g++ -w -O -c reminder.cpp
         memo : memo.cpp reminder.h reminder.o
         	g++ -w -O -o memo memo.cpp reminder.o

      Advanced Makefiles

      The target doesn't have to be a real file. For example
        	rm *.o
      means that the command make clean removes all the .o files.

      You can define a short name for something:

       	CC=g++ -w -o
      means that $(CC) can be used to replace g++ -w -o in any command. Note. Error in Horstmann: When CC is in a command it has to be written $(CC) not CC.

      We can generate large numbers of recipes by using implicit rules. They are hard to understand but easy to use:

        	$(CC) -c $<
      has the effect of creating an infinite number of similar rules: they will take any .cpp file and make a .o file from it.

      See Also

      For more information see my local documentation in http://cse.csusb.edu/dick/doc/: [ make.html ] [ makefile.FAQ.txt ] [ make.man ]

      BNF Syntax

    1. makefile::= # recipe. Any number of recipes.
    2. recipe::= header # step. A recipe has one header and any number of steps.
    3. header::= target colon # input endl. A header has a target, a colon, and a number of inputs on one line.
    4. step::=htab Unix_command endl. A step must be prefixed by a tab character and be on one line.
    5. target::= identifier.
    6. colon::=":".
    7. input::=identifier.
    8. endl::=end of a line, tap the enter key!.
    9. htab::=Horizontal tab, tap the key marked Tab.
    10. Unix_command::=Any command that a UNIX shell will execute.

    . . . . . . . . . ( end of section Make) <<Contents | End>>


  1. accessor::=`A Function that accesses information in an object with out changing the object in any visible way". In C++ this is called a "const function". In the UML it is called a query.
  2. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  3. class::="A description of a set of similar objects that have similar data plus the functions needed to manipulate the data".
  4. constructor::="A Function in a class that creates new objects in the class".
  5. Data_Structure::=A small data base.
  6. destructor::=`A Function that is called when an object is destroyed".
  7. Function::programming=A selfcontained and named piece of program that knows how to do something.
  8. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  9. mutator::="A Function that changes an object".
  10. object::="A little bit of knowledge -- some data and some know how". An object is instance of a class.
  11. objects::=plural of object.
  12. OOP::="Object-Oriented Programming", Current paradigm for programming.
  13. Semantics::=Rules determining the meaning of correct statements in a language.
  14. SP::="Structured Programming", a previous paradigm for programming.
  15. STL::="The standard C++ library of classes and functions" -- also called the "Standard Template Library" because many of the classes and functions will work with any kind of data.
  16. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  17. Q::software="A program I wrote to make software easier to develop",
  18. TBA::="To Be Announced", something I should do.
  19. TBD::="To Be Done", something you have to do.
  20. UML::="Unified Modeling Language".
  21. void::C++Keyword="Indicates a function that has no return".