[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] / [CSE201] / 15
[Text Version] [Syllabus] [Schedule] [Glossary] [Labs] [Projects] [Resources] [Grading] [Contact] [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]
Tue May 7 09:14:31 PDT 2013
Opening the PDF files on this page may require you to download Adobe Reader or an equivalent viewer (GhostScript).


    cs201/15 -- Multifile Compilations

      Previous -- Classes

      [ 14.html ]

      Study Multifile Compilation pages 253-258

        Large projects have to be split into many small files

        [ ../src/lisp/ ]

        Quality Tip 5.1 Laying out a file of code

        Organize your files like this:
        1. /* who wrote what, when and why */
        2. #include header files
        3. declare constants
        4. declare classes
        5. declare functions

        Productivity hint: write yourself a personal "fill-in-the-blanks" file.

        Syntax for including library files

         	#include <name>

        Syntax for including your own local files

         	#include "name.h"

        5.9 Separate Compilation

          I have placed the "prodtest" source code on the web in directory [ prodtest/ ] and these files [ prodtest/product.h ] (header) [ prodtest/product.cpp ] (source) [ prodtest/prodtest.cpp ] (test) and a [ prodtest/Makefile ] ( [ Makefiles and make ] later ) to make compilation easy on Linux.

          A C++ component is split into to separate files. One declares the interface for the component -- how to access it. The other defines how the functions work. The interface declares function headers. It is also called a header file. As a result the interface to a C++ class is put in a file that ends

          (for header).

          Header files have this weird set of directives at the beginning and the end.

           	#ifndef NAME_H
           	#define NAME_H
          This means that the compiler only reads the files content "IF NAME_H is Not DEFined", and if the content is read the next statement defines "NAME_H".

          The source code files should always "#include" the header file.

          Notice the different command used to compile a source code component

           		g++ -c name.cpp

          Notice the command used to assemble and output a program from several files is the one we have been using for some time:

           		g++ -o program source1.o source2.o ... main.cpp

          Magic -- the modular files

          We started with [ magic0.cpp ] but to make this part of a big project we would split it up like this [ magic.h ] (interface), [ magic.cpp ] (definitions of functions), and [ test.magic.cpp ] (testing).

          Exercise -- extracting reusable procedures -- sort3 revisited

          What files can we extract and make reusable in [ sort3c.cpp ] ?

        Makefiles and make

          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.

          Name of a Makefile

          You can either call it "makefile" or "Makefile". But it is confusing to have both.

          Place of a Makefile

          Have one makefile/Makefile for each directory that contains source code. The command "make" always looks in the current working directory for its makefile.


          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 -- memo

          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 -- memo needs remind

          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 -- compiling remind

          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 need two recipes, like this:
             memo : memo.cpp reminder.h reminder.o
             	g++ -w -O -o memo memo.cpp reminder.o
             reminder.o : reminder.cpp reminder.h
             	g++ -w -O -c reminder.cpp

          Example 4 -- A Banking Application

          Programming an ATM promises to be quite a complicated task. We need to organize our code in a way that separates the variaous parts of the software -- and lets us test them (unit tests) before we combine them with other. Part of the problem is keeping track of a customer's account. So we invent a class called Account and define the following interface [ Account.h ] and write a test [ test.Account.cpp ] for this class. We will also be needing to implement the functions in "Account.h" and here [ Account.cpp ] is a start.

          To make the project easier we also need a Makefile that includes these recipes:

           test: test.Account
           test.Account: test.Account.cpp Account.o
           	g++ -o test.Account test.Account.cpp Account.o
           Account.o: Account.cpp Account.h
           	g++ -c Account.cpp

          Exercise -- explain what each line of the Makefile does

          Demo in class of developing the class

          Example 5 -- The Counter class needs a Makefile

          [ test.Counter.cpp ] [ Counter.h ] [ Counter.cpp ]

          See Also

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

          Simplified BNF Syntax

          I'm leaving out actions below.

        1. makefile::= recipes.
        2. recipes::= # recipe. Any number of recipes.
        3. recipe::= header # step. A recipe has one header and any number of steps.

        4. header::= target colon # input endl. A header has a target, a colon, and a number of inputs on one line.

        5. step::=htab Unix_command endl. A step must be prefixed by a tab character and be on one line.

        6. target::= identifier.
        7. colon::=":".
        8. input::=identifier.
        9. endl::=end of a line, tap the enter key!.
        10. htab::=Horizontal tab, tap the key marked Tab.
        11. Unix_command::=Any command that a UNIX shell will execute.

          Exercise -- a Makefile for sort3

          Previously we split up [ sort3c.cpp ] into 3 files... write a Makefile that describes how to compile and test them.

        . . . . . . . . . ( end of section Makefiles and make) <<Contents | End>>

        Random Fact 5.2 Art or science

        So what do you mean by art?

        Don't you think that Einstein looks more like an artist than a lab-coated scientist?

        George Seurat: I just apply my method.

        Should an artist design a pace maker?

        Do scientists design things? Why?

        As part of my graduate work I was involved in the Brittish Computer Arts Society and even exhibitted some of my work... was I being a scientist or an artist?

        Professional Societies -- ACM and IEEE CS

        The book is wrong in one respect. We do have professional societies with ethical standard. The Association for Computing Machinary and the IEEE Computer Society are both fill this niche. Most faculty are members of at least one of these. They also run the CSE accreditation via "ABET". Finally, they have student chapters on this campus. CSE majors should join them. Ask any faculty...

        Review Questions

          Do as many as you can of the following and hand in one.

        1. Give two reasons for splitting a program into several files.
        2. What do you expect to find in a file with suffix ".cpp", ".h", and ".o"?
        3. Write a simple Makefile (HowTo on this page) for the multifile program in this section of the chapter. The commands are on page 256 but you should end up describing three recipes.

        . . . . . . . . . ( end of section Review Questions) <<Contents | End>>

      Lab 8 -- Multifile compilation

      [ lab08/ ]

      Hand in a one or two sentence description at the start of the next class.

      A simple example of an included class -- A Bag of M_n_Ms

      This example is fun: [ m_n_ms.cpp ] and [ Randomizer.h ] , but it is not the best style. First it does not use the cstdlib random number functions and second it does not separate the header information from the member function definitions ready for separate compilation.

      Optional exercise for masochists -- edit the above files so that the classes and files follow the structure illustreted in the book. Also -- write a Makefile.

      Project 7 Due next time -- multifile program

      [ projects.html#P7 ]

      Quiz 7 Next time -- fill in blanks in a class

      Next -- UML

      [ 16.html ] Handout: [ uml.html ] [ uml.pdf ]


  1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  2. Class::=A description of a type of object that includes the data it knows and the functions it can execute.
  3. Function::programming=A selfcontained and named piece of program that knows how to do something.
  4. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  5. OOP::="Object-Oriented Programming", Current paradigm for programming.
  6. Semantics::=Rules determining the meaning of correct statements in a language.
  7. SP::="Structured Programming", a previous paradigm for programming.
  8. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  9. Q::software="A program I wrote to make software easier to develop",
  10. TBA::="To Be Announced", something I should do.
  11. TBD::="To Be Done", something you have to do.
  12. UML::="Unified Modeling Language", industry standard design and documentation diagrams.
  13. void::C++Keyword="Indicates a function that has no return value".

( End of document ) <<Contents | Top