Skip to main content

Unit 0.4.4 How we use makefiles

Example 0.4.1.

When one wants to execute an implementation of matrix-matrix multiplication in, for example, file Gemm_IJP.c that will be discussed in Unit 1.1.1, a number of steps need to come together.

  • We have already discussed the driver routine in Assignments/Week1/C/driver.c. It is compiled into an object file (an intermediate step towards creating an executable file) with

    gcc <options> -c -o driver.o driver.c

    where <options> indicates a bunch of options passed to the gcc compiler.

  • The implementation of matrix-matrix multiplication is in file Gemm_IJP.c which is compiled into an object file with

    gcc <options> -c -o Gemm_IJP.o Gemm_IJP.c

  • A routine for timing the executions (borrowed from the libflame library is in file FLA_Clock.c and is compiled into an object file with

    gcc <options> -c -o FLA_Clock.o FLA_Clock.c

  • A routine that compares two matices so that correctness can be checked is in file MaxAbsDiff.c and is compiled into an object file with

    gcc <options> -c -o MaxAbsDiff.o MaxAbsDiff.c

  • A routine that creates a random matrix is in file RandomMatrix.c and is compiled into an object file with

    gcc <options> -c -o RandomMatrix.o RandomMatrix.c

  • Once these object files have been created, they are linked with

    gcc driver.o Gemm_IJP.o FLA_Clock.o MaxAbsDiff.o  RandomMatrix.o 
           -o driver_IJP.x <libraries>

    where <libraries> is a list of libraries to be linked.

Together, these then create the executable driver_IJP.x that can be executed with


This should be interpreted as "executed the file driver.x in the current directory." The driver is set up to take input:

% number of repeats:3
% 3
% enter first, last, inc:100 500 100
% 100 500 100

where "3" and "100 500 100" are inputs.

Many decades ago, someone came up with the idea of creating a file in which the rules of how to, in this situation, create an executable from parts could be spelled out. Usually, these rules are entered in a Makefile. Below is a video that gives a quick peek into the Makefile for the exercises in Week 1.

The bottom line is: by using that Makefile, to make the executable for the implementation of matrix-matrix multiplication in the file Gemm_IJP.c all one has to do is type

make IJP

Better yet, this also then "pipes" input to be used by the driver, and redirects the output into a file for later use when we plot the results.

You can read up on Makefiles on, where else, Wikipedia:

Remark 0.4.2.

Many of us have been productive software developers without ever reading a manual on how to create a Makefile. Instead, we just copy one that works, and modify it for our purposes. So, don't be too intimidated!