Training, Open Source Programming Languages

This is page ... learn.html

Our email: • Phone: 01225 708225

Retiring, March 2020 - sorry, you have missed our final public course.
The Coronavirus situation has lead us to suspend public training - which was on the cards anyway, with no plans to resume

Please ask about private 'maintenance' training for Python, Tcl, Perl, PHP, Lua, etc
Happily continuing private consultancy / programming work
Make and makefiles - a commented example to help you learn

If you're making an executable C++ file you build it from a whole lot of .o files, each of which you have compiled from a .cpp file. And you also build in library files. If you change a .cpp file, then you have to recompile it into a .o ... but don't forget that you also have to recompile the .cpp if you have changed any of the header files it includes. And the final build from .o to executable can be a long statement.

You might well say "oh - for goodness sake - put all the compile and build instructions in a batch file / shell script and run that when you change anything" and that can be quite effective for a small application but as the application grows:
• You won't want to recompile everything every time and
• You won't want to duplicate large numbers of similar compile instructions into your script.

This is where a Makefile comes in to play. A Makefile contains a series of instructions of the form "If file xxx does not exist, or is older than file yyy [from which it is created] the run the following instruction to (re)create file xxx". And the make utility applies this whole setup of dependecnises to work out what needs to be done, and goes off and does it.

For the straightforward Multiple Inheritance in C++ Example that I wrote about earlier, I provided a simple Makefile [here]. It contains directives of the sort:
  Expense.o:        Expense.h Expense.cpp
        g++ -c Expense.cpp

which says that if Expense.cpp or Expense.h have changed more recently that Expense.o, you need to (re)run the gcc line that follows.

There are, though, many short cuts you can use in a Makefile to make it more compact, easier to edit, and to reduce duplication in it. Unfortunately, they make it far harder for the uninitialted to read the file. And it seems that this is a technology which Geeks tend to keep - "Secret Squirrel" to themselves; comments in makefiles are about as rare as daffodils in September!

I have taken the Makefile for the C++ multiple inheritance demo, and I have added in many of the more advanced techniques to show what can be done - and I've added comments too. You can find the complete Makefile [here] ... and you might like to note:

• I can define Macros with assignments
  HEADERS=Film.h HireFilm.h Expense.h iostream
which I can then use further down my Makefile with a $(....) notation
  Filmtest.o: Filmtest.cpp $(HEADERS)
This is commonly done not only for batches of header and object files, but also for the name of the C compiler and flags to the compile and load - typically CC=, CFLAGS= and LFLAGS=.

• As a special case, I can ask Make to look in a path for dependencies using VPATH
and I can add other directories for files who's names match a pattern using vpath [lower case]:
  vpath %.txt ../docs
will say that make should look into the sibling docs directory for .txt file (note the use of % as a wild card in makefiles, rather like in MySQL).

• I can take a list of file names of one particular type and turn it into a list of file names of a different type:
  HIGHSOURCE=Filmtest.cpp HireFilm.cpp
Defines HIGHSOURCE as being some source files, and HIGHOBJ as being the associated object files. Just add to the source list and you automatically add the the object list!

• The SUFFIXES rule(s) allow you to create a set of generic rules telling make how to turn one set of files (by extension) into another. First define the suffixes:
  .SUFFIXES: .html .txt
then the rules:
      @echo "<html><body>" > $@
      @cat ../docs/$< >> $@
      @echo "</body></html>" >> $@
      @echo "Web Page $@ created"

So in that example, if you ask the system to make a .html it will top and tail a .txt file from the .docs directory with html headers and footers. In this example,also note:
* @ to start a command means "do not echo this instruction"
* $< is used to refer to the incoming file for the conversion
* $@ is used to refer to the outgoing or target file.

• The first target in the file will be the one to be made if you don't give make any parameters, and if that target isn't a file name, the target will always be run.
  all: Filmtest
     @echo "The whole caboodle is now up to date"
In this case, with no file called "all", the code will always be run and you'll get the message

• You can define your own rules that are going to be used for most conversions of particular cases - here's a rule that shows how most .o files are made from .cpp files:
  %.o: %.cpp %.h
     $(CC) -c $(CFLAGS) -o $@ $<
Although that's a fallback rule and so it will be overridden where you provide an explicit rule.

• Finally, it's common to provide a dummy target "clean" to delete all temporary and interim files so that you can force a rebuild from scratch:
     @-rm *.o
     @-rm Filmtest
     @-rm *.html
     @echo "Clean dup"

In this case, the extra "-" sign tells make to carry on even if there was an error!

Here is an example of that makefile being run:
[trainee@easterton 007]$ make
g++ -c -O2 -pg Filmtest.cpp
g++ -c -O2 -pg HireFilm.cpp
g++ -c -O2 -pg -o Film.o Film.cpp
g++ -c -O2 -pg -o Expense.o Expense.cpp
g++ -o Filmtest -pg Filmtest.o HireFilm.o Film.o Expense.o
The whole caboodle is now up to date
[trainee@easterton 007]$ make
The whole caboodle is now up to date
[trainee@easterton 007]$ make about.html
Web Page about.html created
[trainee@easterton 007]$ make about.html
make: `about.html' is up to date.
[trainee@easterton 007]$

(I have added the -pg option since I uploaded the sample make file to the web site, as I was demonstrating how to set up and use tools such as the gprof profiler and the indent code tidy utility, but those are probably subjects for another day.)

There are many more makefiles in our sample directories - - - - - - - - - - - - - - - - - -. Each dash links to a different one and by adding links here our web site software automatically adds links back to this page ;-)
(written 2010-03-12, updated 2010-03-13)

Associated topics are indexed as below, or enter for individual articles
A168 - Web Application Deployment - Compiler and development tools
  [4585] What is make? What is gcc? - (2015-11-28)
  [4013] Web Frameworks - nested templates - (2013-02-22)
  [3666] Makefile variables - defined internally, from the command line and from the environment - (2012-03-22)
  [3658] Using Make for a distribution - (2012-03-17)
  [3652] A Complete makefile example - (2012-03-14)
  [3651] Makefile - some basics, and a demonstration - (2012-03-13)
  [3632] What is Make? - (2012-03-02)
  [3053] Make - automating the commands for building and installing - (2010-11-16)
  [1671] Compiling C programs with gcc - an overview - (2008-06-10)
  [694] Ant and Make - (2006-04-22)

C239 - C and C based languages - Putting it all together
  [4559] When do I use the this keyword in C++? - (2015-10-29)
  [4374] Test driven development, and class design, from first principles (using C++) - (2014-12-30)
  [4326] Learning to program - comments, documentation and test code - (2014-11-22)
  [3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
  [3252] C++ - unknown array size, unknown object type. Help! - (2011-04-17)
  [3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
  [3067] Using C and C++ functions in the same program - how to do it - (2010-11-24)
  [2851] Further C++ material - view new or old - (2010-07-04)
  [2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
  [2646] Compile but do not run PHP - syntax check only - (2010-02-22)
  [1181] Good Programming practise - where to initialise variables - (2007-05-09)
  [945] Code quality counts - (2006-11-26)
  [925] C++ - just beyond the basics. More you can do - (2006-11-14)
  [836] Build on what you already have with OO - (2006-08-17)

Back to
Multiple Inheritance in C++ - a complete example
Previous and next
Horse's mouth home
Forward to
Redirecting to your main domain for correct security keys
Some other Articles
Reaching out to the community - a good case for support
Blowing out the winter cobwebs
Changing Times
Redirecting to your main domain for correct security keys
Make and makefiles - a commented example to help you learn
Dear Planners, please provide viable alternatives
Melksham - Carnival, Party in the Park, and Spot the Oddity
Pointers to Pointers to Pointers - what is the point?
Efficient use of dynamic memory - C and realloc
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2020: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • EMAIL: • WEB: • SKYPE: wellho

PAGE: ... learn.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat