Makefiles
Makefiles
Makefiles
Nolan Bard
Overview
1 What is a Makefile?
Explanation
Motivation
2 Creating and Using Makefiles
Makefile Creation
Using make
Simple Example
3 Try it out!
My First Makefile
4 Makefile Variables
Defining Variables
Using Variables
Overview
7 Concluding Notes
Conclusion
Resources
Part I
What is a Makefile?
Part II
$ cat Makefile
#This is a comment
#This is a comment that goes on and on and on. But I can make it look \
nicer by splitting it over multiple lines using a backslash.
Makefiles also allow for comments in the file. Simply use a # and everything
from it to the end of the line will be ignored by make.
Sometimes you will have long lines in a makefile. To keep this more legible you
can use a backslash to continue the current line on the next line.
$ cat Makefile
#This is a comment
#This is a comment that goes on and on and on. But I can make it look \
nicer by splitting it over multiple lines using a backslash.
Makefiles also allow for comments in the file. Simply use a # and everything
from it to the end of the line will be ignored by make.
Sometimes you will have long lines in a makefile. To keep this more legible you
can use a backslash to continue the current line on the next line.
Simple Example
all: myProgram
myProgram: myProgram.c
gcc -Wall -ansi myProgram.c -o myProgram
Part III
Try it out!
Part IV
Makefile Variables
Defining Variables
all: myProgram
myProgram: myProgram.o
gcc -Wall -ansi myProgram.o -o myProgram
Using Variables
all: myProgram
myProgram: myProgram.o
$(CC) $(CFLAGS) myProgram.o -o myProgram
Using Variables
Using Variables
Using Variables
Part V
$ cat Makefile
CC = gcc
CFLAGS = -Wall -ansi
TARBALL_NAME = submit.tar
all: myProgram
myProgram: myProgram.o
$(CC) $(CFLAGS) myProgram.o -o myProgram
handin: $(TARBALL_NAME)
astep -c c201 -p assn1 $(TARBALL_NAME)
clean:
-rm -f core *.o
Part VI
Using the makefile and the Hello World program you already
made, we will now try out some of the other makefile
techniques.
In your hello.c file, add ’ #include ”memwatch.h” ’
Open your makefile. We want to:
compile hello.c with memwatch.c
remove duplication in our makefile
be able to clean up our files
be able to submit easily
Using the makefile and the Hello World program you already
made, we will now try out some of the other makefile
techniques.
In your hello.c file, add ’ #include ”memwatch.h” ’
Open your makefile. We want to:
compile hello.c with memwatch.c
remove duplication in our makefile
be able to clean up our files
be able to submit easily
Using the makefile and the Hello World program you already
made, we will now try out some of the other makefile
techniques.
In your hello.c file, add ’ #include ”memwatch.h” ’
Open your makefile. We want to:
compile hello.c with memwatch.c
remove duplication in our makefile
be able to clean up our files
be able to submit easily
A Final Product
CC = gcc
DEFS = -DMEMWATCH -DMW_STDIO
CFLAGS = -Wall -ansi $(DEFS)
EXEC_NAME = hello
TARBALL_NAME = submit.tar
all: $(EXEC_NAME)
$(EXEC_NAME): $(OBJECTS)
$(CC) $(CFLAGS) $(OBJECTS) -o $(EXEC_NAME)
$(TARBALL_NAME): Makefile README $(BASIC_C) $(BASIC_H)
tar -cvf $(TARBALL_NAME) Makefile README $(BASIC_C) $(BASIC_H)
handin: $(TARBALL_NAME)
astep -c c201 -p assn1 $(TARBALL_NAME)
clean:
-rm -f core *.o $(EXEC_NAME)
Concluding Notes
Conclusion
Resources