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
make
make test
make install
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.
The
make 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.
make test 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 articles
C231 - C and C based languages - Introduction to C++ [317] Programming languages - a comparison - (2005-05-20)
[318] Choosing a theme - (2005-05-20)
[336] Targetted Advertising - (2005-06-05)
[928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
[2004] Variable Scope in C++ - (2009-01-22)
[2169] When should I use OO techniques? - (2009-05-11)
[2845] Objects and Inheritance in C++ - an easy start - (2010-07-01)
[3052] Getting your C++ program to run - (2010-11-15)
[3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
[3250] C++ - how we teach the language and the concepts behind the language - (2011-04-17)
[4466] Moving from C to C++ - Structured to Object Oriented - a lesson for engineers - (2015-03-28)
[4561] Hello World in C++ - a first program, with the process explained - (2015-10-30)
[4562] Left shift operator on an output stream object - C++ - (2015-10-30)
C050 - C and C based languages - C - General [2002] New C Examples - pointers, realloc, structs and more - (2009-01-20)
[2086] C Programming v Learning to Program in C. Which course? - (2009-03-17)
[2091] C, C++ and C# ... Java and JavaScript - (2009-03-20)
[2504] Learning to program in ... - (2009-11-15)
[2536] All the Cs ... and Java too - (2009-12-13)
[2669] Efficient use of dynamic memory - C and realloc - (2010-03-10)
[2763] Our C and C++ training course are on Open Source platforms - (2010-05-13)
[2848] C course - final course example puts it all together - (2010-07-02)
[3129] Extra courses - C and C++ - (2011-01-12)
[4335] Flexible public courses - residential or commuting, programming newcomer or experienced, C or C++ - (2014-11-30)
[4341] Segmentation Fault, Segmentation Violation, Bus Error, Stack Smashing - (2014-12-04)
[4434] Public training courses - upcoming dates - (2015-02-21)
A168 - Web Application Deployment - Compiler and development tools [694] Ant and Make - (2006-04-22)
[1671] Compiling C programs with gcc - an overview - (2008-06-10)
[2674] Make and makefiles - a commented example to help you learn - (2010-03-12)
[3632] What is Make? - (2012-03-02)
[3651] Makefile - some basics, and a demonstration - (2012-03-13)
[3652] A Complete makefile example - (2012-03-14)
[3658] Using Make for a distribution - (2012-03-17)
[3666] Makefile variables - defined internally, from the command line and from the environment - (2012-03-22)
[4013] Web Frameworks - nested templates - (2013-02-22)
[4585] What is make? What is gcc? - (2015-11-28)
A165 - Web Application Deployment - Installing Software [809] What to do during a Linux build - (2006-07-20)
[1700] FTP server on Fedora Linux - (2008-07-06)
[2139] OS Commerce install made simple - (2009-04-24)
[2201] Running straight from the jar, but not from a tar - (2009-05-26)
[4259] Upgrading our training systems to all the current stable versions - (2014-04-07)
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