Unix Reference

make (compliles your program using a set of commands)

Video

In cs107, we will use a program called make to compile our programs. Make is a program that dates back to 1976, and it is used to build projects with dependencies such that it only recompiles files that have been changed.

For our purposes, you will not need to know too much about Make, except how to use it. What is very important, however, is that you need to remember to run make after any change to the source code of your programs -- many students forget to run make and wonder why they get unexpected results from their programs, when it is simply that they never re-compiled their code after the changes.

The most simple way to use make is by just typing make in a directory that contains a "Makefile" called, fittingly, Makefile:

$ make
gcc -g -O0 -std=gnu99  -o hello helloWorld.c helloLanguages.c
$ ./hello
Hello World
Hallo Welt
Bonjour monde

The Makefile above compiled the program with two .c files into a runnable program called hello.

Here is a more full look at the details that went into the compilation above. The Makefile has rules that are followed to decide when to compile a program. In particular, the hello: line in the Makefile tells Make to re-compile the program if any of the three files (hello.c, helloLanguages.c, and hello.h) have changed. On the following line, which must begin with a tab and not spaces, the compilation line runs. There are two variables in this Makefile, CC (the compiler), and CFLAGS (the flags that we are going to send to the compiler).

See how to compile with gcc for information about how the compilation happens.

$ ls
hello.h  helloLanguages.c  helloWorld.c  Makefile
$ cat Makefile
#
# A very simple makefile
#

# The default C compiler
CC = gcc
CFLAGS = -g -O0 -std=gnu99

hello: helloWorld.c helloLanguages.c hello.h
    $(CC) $(CFLAGS) -o hello helloWorld.c helloLanguages.c

clean:
    rm -f hello *.o

$ cat hello.h
#include<stdio.h>
#include<stdlib.h>

void helloEnglish();
void helloGerman();
void helloFrench();

$ cat helloWorld.c
#include "hello.h"

int main() {
    helloEnglish();
    helloGerman();
    helloFrench();
    return 0;
}

$ cat helloLanguages.c
#include "hello.h"

void helloEnglish() {
    printf("Hello World\n");
}

void helloGerman() {
    printf("Hallo Welt\n");
}

void helloFrench() {
    printf("Bonjour monde\n");
}


$ make
gcc -g -O0 -std=gnu99  -o hello helloWorld.c helloLanguages.c
$ ./hello
Hello World
Hallo Welt
Bonjour monde

Back to contents