Coding projects (and their open source distributions) require build scripts - files of commands that convert the source code into executable programs, that install the various resultant files into place, and that can clean up the build area for a fresh build or for redistribution. The make
system - with makefiles
- is the usual way to do this (in the case of C and C++ programs anyway - in the Java world, Ant
is the more usual approach).
How do build systems work?
You specify a target
(the file you want to build), some dependencies
(the files which - if changed - necessitate a rebuild) and a set of rules
for making the target. You build a whole series of sets of targets, dependencies and rules with the target of one set of rules - the name of the output file from a set of rules - being amongst the dependencies (inputs) at the next level. Then when you say "make" the first target in your Makefile
, and anything it depends on too, is rebuilt.
Make is very clever in that it examines the whole build tree before it does anything, so that rules are followed all the way back to see which need to be run, the they're run all the way forward so that they happen (a) only if necessary and (b) in the right order. File timestamps are used to check what has changed, so if you want to force one of the sets of rules to be run, you can simply touch
one of the dependencies and rerun make
There is a commented example of a Makefile - showing and describing its format - [here]
It's the nature of a compile and build operation in a language such as C or C++ that a very similar set of operations will be performed on a whole series of files - for example, there may be dozens of .cpp files to be compiled into .o files, all with the same compiler and compile time options. So to make your makefile shorter and easier to maintain for consistency, you can specify make variables
and make default rules
. There's a further example that shows these features [here]
- again that's commented and kept fairly short and straight forward to help the newcomer.
When you download an open source distribution, please READ THE INSTALL GUIDE before you build and install it. You'll find that in a file called INSTALL
or something similar in the unpacked distribution. And you'll often find the procedure goes like this:
./configure --with-summat --enable-summat-else --etc
With C and C++ programs which run across different flavors of operating systems, your makefile needs to be updated to ensure that the resulting executables make good use of what the OS offers, and that the build doesn't attempt to use facilities that aren't available. You also need the ability to choose options to build in and out, and perhaps to specify target directory paths. The ./configure script / program takes your various requests, analysis and looks round the system on which you're doing the build. It advises you of any dependencies that are missing (i.e. other bits of software you'll need to install files) and outputs a Makefile.
operation does the actual compiles and builds; that will usually include other subsidiary targets to unpack and prepare documentation directories, configuration files for the application itself when run, etc.
will put the newly build application through a whole series of checks to ensure that the output is 100% fine - this is designed to catch anything that hasn't worked as it should in the build process, and may also serve to give you an early alert of facilities that are missing or non-functional on your particular operating system / configuration. The output of make test is a report, so if you're repeating the same build across a large number of systems you might consider skipping it; on occasions that we're rebuilding a series of systems, we sometimes do - but then we're building for a week's training and not for systems that are going to sit in an exposed production environment for perhaps years.
The final step - make install
- copies the output of make
from the staging / development area where make
wrote its output to the production area. You'll usually need to be logged in as the administrator - for this step only!
- and you should remember to get yourself a full root environment. In other words - do not use su
without any options; use su -
(a lone minus option), even though the latter moves you to root's home directory and having to cd
back to your build area.
P.S. Before you make install
you should check that any prior versions that you're going to overwrite aren't actually running. Also consider using a fresh parallel install directory as that will give you the ability to rollback if you find the need. (written 2010-11-16, updated 2010-12-04)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesA165 - Web Application Deployment - Installing Software 
Upgrading our training systems to all the current stable versions - (2014-04-07) 
Running straight from the jar, but not from a tar - (2009-05-26) 
OS Commerce install made simple - (2009-04-24) 
FTP server on Fedora Linux - (2008-07-06) 
What to do during a Linux build - (2006-07-20)A168 - Web Application Deployment - Compiler and development tools 
Web Frameworks - nested templates - (2013-02-22) 
Makefile variables - defined internally, from the command line and from the environment - (2012-03-22) 
Using Make for a distribution - (2012-03-17) 
A Complete makefile example - (2012-03-14) 
Makefile - some basics, and a demonstration - (2012-03-13) 
What is Make? - (2012-03-02) 
Make and makefiles - a commented example to help you learn - (2010-03-12) 
Compiling C programs with gcc - an overview - (2008-06-10) 
Ant and Make - (2006-04-22)C050 - C and C based languages - C - General 
Extra courses - C and C++ - (2011-01-12) 
C course - final course example puts it all together - (2010-07-02) 
Our C and C++ training course are on Open Source platforms - (2010-05-13) 
Efficient use of dynamic memory - C and realloc - (2010-03-10) 
All the Cs ... and Java too - (2009-12-13) 
Learning to program in ... - (2009-11-15) 
C Programming v Learning to Program in C. Which course? - (2009-03-17) 
New C Examples - pointers, realloc, structs and more - (2009-01-20)C231 - C and C based languages - Introduction to C++ 
C++ - how we teach the language and the concepts behind the language - (2011-04-17) 
Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25) 
Getting your C++ program to run - (2010-11-15) 
Objects and Inheritance in C++ - an easy start - (2010-07-01) 
When should I use OO techniques? - (2009-05-11) 
Variable Scope in C++ - (2009-01-22) 
C++ and Perl - why did they do it THAT way? - (2006-11-16) 
Targetted Advertising - (2005-06-05) 
Choosing a theme - (2005-05-20) 
Programming languages - a comparison - (2005-05-20)
Some other Articles
Lots of things to do with and within a C++ classC++ - a complete example with polymorphism, and how to split it into project filesZyliana Kyrei CoxLonghope HotelMake - automating the commands for building and installingPositively reforming the system - could it be done?Views of Melksham - November 2010Computer Graphics is fun - even if Java Applets are Old HatString handling - from first steps to practical examples