Skip to content

Makefiles Outside of Classes (Makefile Idioms)

markisaa edited this page Mar 6, 2015 · 2 revisions

For your classes there are some restrictions on what you are allowed to put in your Makefile. The autograder for 281, for instance, will not allow built-in rules. That said, the idiomatic and "right" way to do things can sometimes differ, but requires a more extensive understanding of the tool, which is why 281 imposed those limitations. Once you understand the fundamentals of how Makefiles work, there's absolutely no reason not to let make do some of the work for you :).

This page covers idiomatic Makefile usage, as opposed to Makefiles for UMich classes.

Minimal Makefiles

As a general rule, you don't need much in your makefiles, there is a lot built in. Try this:

$ ls
hello_world.c
$ cat hello_world.c
#include <stdio.h>
int main() { printf("hello world\n"); }
$ make hello_world && ./hello_world
cc     hello_world.c   -o hello_world
hello world

Notice there is no Makefile in this directory, you do not need one for local development (though you would for submitting to an autograder). Make has a lot of rules built in for how source files should turn into executables. These rules are generally TheRightWay (TM) to do things. So, DO NOT define the compiler and DO NOT write rules for compiling c/c++ files. DO add edit the variables CFLAGS/CXXFLAGS as needed (e.g., CFLAGS += -m32). DO add targets and clean rules, e.g.

all:    hello_world

clean:
        rm -f hello_world

.PHONY: all clean

A full example