Open main menu

CDOT Wiki β

Changes

Make and Makefiles

1,083 bytes added, 13:12, 13 September 2017
Examples
Running the <code>make</code> command by itself will execute the makefile script named <code>Makefile</code> or <code>makefile</code> in the current directory.
== Targets and Dependencies ==
Picture a very simple build, where the file <code>test.c</code> is compiled by ''gcc'' into the executable binary named <code>test</code>.
This is because the timestamp on the target (<code>test</code>) is later than the timestamp on the dependency (<code>test.c</code>). If the dependency has been changed since the target was built, though, then ''make'' will rebuild the target.
=== Complex Dependencies ===
A more complicated build will involve a number of targets and dependencies. C programs, for example, can be compiled into intermediate files, called object files (.o extension), which can then be combined to produce executables.
make: Nothing to be done for 'all'.
If the file <code>half.c</code> was edited or the datestamp last-modified timestamp (mtime) was updated, running ''make'' would execute two compilations:
$ touch half.c
On a large programming project, a binary may be comprised of hundreds or even thousands of source files, and compiling all of those files may take hours. If a software developer edits just one file, it's a waste of time to rebuild everything, so ''make'' can save a lot of time -- especially when the software is rebuilt many thousand times.
 
=== Fake Targets ===
 
It is not uncommon to include "fake" targets in a Makefile -- targets which never get built, but which perform a useful operation. For example, a target of "all" never produces an actual file named "all". Typical fake targets include:
* all: build all binaries
* docs: builts all documentation (e.g., generates PDFs, HTML, manpages, etc)
* install: install all files, building binaries, documentation, etc if required
* clean: erases all built intermediate and binary files
* dist-clean (or distclean): erases all files not included in the original distribution of the source
* check (or test): tests the software
 
The make command will exit as soon as any command pipeline fails, so fake targets which may non-fatally fail are usually forced to return a success code; for example, to delete files which may or may not exist as part of a "clean" target, code such as this may be used:
 
rm *.o || true
 
== Examples ==
 
* [http://matrix.senecacollege.ca/~chris.tyler/osd600/makefile-examples.tgz A tarball of simple Makefile examples]