User Tools

Site Tools


guides:software:make:start

This is an old revision of the document!


Make

Make is a build automation tool that is designed to automatically construct executable programs from source code.

Simple Use

Make has a set of built in rules that allow you to compile single file programs with no additional configuration. The only requirement is that you provide the correct extension to your source code file. Make uses this extension to decide which compiler to run.

Make recognizes files ending with .cpp, .C and several other extensions as c++ source code. To build an executable from a file with an appropriate extension type make progname. In the following example, the user wishes to construct the executable hello from the program hello.cpp.

$ ls
hello.cpp
$ make hello
g++     hello.cpp   -o hello
$ ls
hello*  hello.cpp

In the example:

  1. The user typed ls to show the files in the directory
  2. The user next typed make hello
    1. This caused make to compile hello.cpp and produce the output file hello.
  3. The user typed ls again to show that the file hello had been produced.

Make will only rebuild an executable if it is necessary. Make looks at the timesamp on the source files and executable to determine if such a rebuild is required. If the executable is newer than the source code, make will not rebuild the executable.

$ ls
hello.cpp
$ make hello
g++     hello.cpp   -o hello
$ make hello
make: 'hello' is up to date.

In this example

  1. The user typed ls to show the contents of the directory
  2. The user typed make hello
    1. make built the executable hello from the source code hello.cpp
  3. The user typed make hello again
    1. Make observed that the time stamp on hello was newer than that on hello.cpp
    2. Based on that observation, make decided that there was no need to rebuild hello

If the user wished to rebuild hello in the above example they could do one of several things:

  • Remove the executable hello (rm hello)
  • Change the timestamp on hello.cpp (touch hello.cpp)
  • Edit the file hello.cpp and save the new file.

Unless directed otherwise, make uses the files in the current directory when attempting to build an executable. In the next example, the user attempts to build an executable when source code is missing.

$ ls
hello*  hello.cpp
$ make bad
make: *** No rule to make target 'bad'.  Stop.

In this example:

  1. The user types ls to show the contents of the directory
  2. The user types make bad to attempt to build an executable bad
    1. Make does not detect a source code file that can be used to construct bad, so it reports an error and exits.

Basic Make Files

Make uses a file called either Makefile or makefile as a configuration file. If one of these two files are present, make will read the this file and use it to determine how to build the executable.

A relatively powerful yet simple use of a Makefile is to provide the proper command line arguments to build your executable.

Makefiles for Multiple File Compilation

guides/software/make/start.1595253508.txt.gz · Last modified: 2022/08/02 11:59 (external edit)