Professional Documents
Culture Documents
Chris' Makefile Tutorial: Contents
Chris' Makefile Tutorial: Contents
Chris Serson
University of Victoria
June 26, 2007
Contents:
Chapter Page
Introduction 2
2 Syntax so far 5
4 Multi-file projects 11
Conclusion 16
2
Introduction:
project2: myfile2.c
gcc -o project2 myfile2.c
<--- end of file
If you add this line to the very top of your Makefile, now
when you type:
> make
it will run your 'all:' target, which points to both of
your 'project_' targets. Of course, you could add the
'all:' target anywhere in the file and you could compile
all of your targets by typing:
> make all
but having it as the first target makes a certain amount of
sense. Another option is to create a 'default:' target at
the top of the file which you could leave empty if you
wanted the default action to do nothing.
In this case:
> make
would do nothing.
5
...
project1: myfile1.c
$(CC) $(CFLAGS) project1 myfile1.c
<--- end of file
# example 1:
project1: myfile1.c extra.h
$(CC) -o project1 myfile1.c
# same as
project1: myfile1.c extra.h
$(CC) -o $@ $^
# or
project1: myfile1.c extra.h
$(CC) -o $@ $<
10
# example 2:
# this will not work. It will give a whole bunch of errors
# since it tries to compile both files and myfile1.c
already
# includes myfile2.c. We effectively get a redefinition of
# everything in myfile2.c. Note: it is possible to write
# your code so this error won't actually happen and this
# line will work.
project1: myfile1.c myfile2.c
$(CC) -o $@ $^
# this will work. It only takes the first file and compiles
# it, linking in the other file automatically as
appropriate.
project1: myfile1.c myfile2.c
$(CC) -o $@ $<
<--- end of file
11
This will work just fine, but what an evil list of file
dependencies. Let's try something different:
program, but you can also see where one section of code is
dependant on another. If you wanted, you could remove the
'queue.h' from 'main.o' and 'list.h' from 'queue.o' and it
would make no difference; however it would not be as clear
the link between each code set.
So what exactly is going on? How does this work? What are
those '.o' files? Basically, we've split the program up
into a bunch of pieces called object files (the '.o'
files). These files make up the entirety of the program but
they don't actually do anything on their own because they
are not complete programs. They depend on the other files
to work. So now what we have to do is link them together.
That's what happens when we make the 'project' target.
When you make the 'project' it runs make on each of the
'.o' file targets. If those '.o' files don't exist or are
not up-to-date, they are recompiled and then linked
together as a final step.
13
Multi-folder projects
Let's say you have a big project and you want to keep your
headers in one folder (/headers) and your source (/source)
in another; and maybe your object files (the '.o's) should
be in their own folder as well (/obj). No problem. Just
make sure your includes are correct in your code and add
another macro:
%.o: %.c
$(CC) -c $^
<--- end of file
%.o: %.c
$(CREATEOBJ)
<--- end of file
15
%.o: %.c
$(CREATEOBJ)
<--- end of file
LFLAGS vs CFLAGS
We've seen the CFLAGS macro used before to define the flags
used in relation to the compiler. But if you've noticed, we
use different flags to compile the object files than we do
to link them together. You may also want to consider a
DEBUG macro that contains flags relating to any debugging
you are doing:
%.o: %.c
$(CREATEOBJ)
<--- end of file
16
Conclusion:
So by now you should have a fair understanding of how a
Makefile works. I've tried to simplify things and give lots
of examples while still giving you a lot to work with. I
hope I've succeeded, but if I haven't then below are a few
websites which I used as references. Also, a websearch for
"Makefile Tutorial" will turn up lots of helpful
information.
1. http://palantir.swarthmore.edu/maxwell/classes/tutorials/maketutor/
2. http://www.hsrl.rutgers.edu/ug/make_help.html
3. http://www.cs.umd.edu/class/spring2002/cmsc214/Tutorial/makefile.html