Lecture Makefile
Lecture Makefile
h files, and
.c files, and .o files, OH MY!
For projects with more complexity.
(Great.. Just what we needed)
1
Breaking your program into files
– main.c
– stack.c
– stack.h
2
Breaking your program into files
• I have an example on
~cs157/class/7_Makefile
– main.c
• The main function, to actually do the “job”
– stack.c
• The code for a stack of integers.
– stack.h
• The “declarations” of a stack of integers.
3
Why break them up?
• main just needs a stack
– It does not want (or need) to care how it is
built or used!
• Smaller files are easier to read
• Faster to compile
– More on this later
• Breaks the program into logical CHUNKS
4
stack.h
typedef struct S_stack {
int number;
struct S_stack *next;
} stack;
• No actual code!
• Just “this is the structure” and
• These are the functions. … “never mind how they work”
5
stack.c
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
6
stack.c
void push(int number, stack **stk_ptr) {
stack *stk, *tmp;
stk = *stk_ptr;
tmp = malloc(sizeof(stack));
tmp->number = number;
tmp->next = stk;
stk = tmp;
*stk_ptr = stk;
}
7
stack.c
int pop(stack **stk_ptr) {
int number;
stack *stk, *tmp;
stk = *stk_ptr;
tmp = stk;
number = tmp->number;
stk = stk->next;
free(tmp);
*stk_ptr = stk;
return number;
}
8
main.c
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
9
main.c
int main() {
stack *stk = NULL;
push(7, &stk);
push(2, &stk);
push(9, &stk);
push(12,&stk);
printf("%d\n",pop(&stk));
printf("%d\n",pop(&stk));
printf("%d\n",pop(&stk));
printf("%d\n",pop(&stk));
printf("%d\n",pop(&stk));
return 0;
}
10
Compiling multiple files (Opt 1)
• gcc –Wall main.c stack.c
– Compiles BOTH files... and makes a.out
• Advantages:
– Easy to remember
• Disadvantages:
– If you have a LOT of .c files, then it becomes
tedious AND slow!
11
Compiling multiple files (Opt 2)
• gcc –Wall –c main.c
– turns main.c into main.o
• gcc –Wall –c stack.c
– turns stack.c into stack.o
• gcc –Wall –o stacktest stack.o main.o
– takes stack.o and main.o and makes
“stacktest” out of them
– Called “LINKING”
12
Whats a .o?
• An “Object File”
• Contains the compiled contents of the
corresponding .c program
• For example:
– stack.o contains the computer-language
version of stack.c
• Can’t turn a .h into a .o (no code in .h)
13
Compiling multiple files (Opt 2)
• Advantages:
– Faster (Only recompile parts then re-link)
• Disadvantages:
– Loads of typing!
14
Makefiles
• Automate the process
• You tell the Makefile:
– What you want to make
– How it goes about making it
• And it figures out
– What needs to be (re) compiled and linked
– What order to do it in
• You just type “make”
15
Makefiles
• Can be HUGELY complex
16
Makefile
CC = gcc
CFLAGS = -Wall
LDFLAGS =
OBJFILES = stack.o main.o
TARGET = stacktest
all: $(TARGET)
$(TARGET): $(OBJFILES)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJFILES) $(LDFLAGS)
clean:
rm -f $(OBJFILES) $(TARGET) *~
17
Makefile
CC = gcc Which compiler to use
CFLAGS = -Wall
LDFLAGS =
OBJFILES = stack.o main.o
TARGET = stacktest
all: $(TARGET)
$(TARGET): $(OBJFILES)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJFILES) $(LDFLAGS)
clean:
rm -f $(OBJFILES) $(TARGET) *~
18
Makefile
CC = gcc Which flags to use
CFLAGS = -Wall -ggdb -Wall etc…
LDFLAGS =
OBJFILES = stack.o main.o
TARGET = stacktest
all: $(TARGET)
$(TARGET): $(OBJFILES)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJFILES) $(LDFLAGS)
clean:
rm -f $(OBJFILES) $(TARGET) *~
19
Makefile
CC = gcc Which libraries to use
CFLAGS = -Wall -lm -lefence etc…
LDFLAGS =
OBJFILES = stack.o main.o
TARGET = stacktest
all: $(TARGET)
$(TARGET): $(OBJFILES)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJFILES) $(LDFLAGS)
clean:
rm -f $(OBJFILES) $(TARGET) *~
20
Makefile
CC = gcc Which object files are
CFLAGS = -Wall part of the final program
LDFLAGS =
OBJFILES = stack.o main.o
TARGET = stacktest
all: $(TARGET)
$(TARGET): $(OBJFILES)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJFILES) $(LDFLAGS)
clean:
rm -f $(OBJFILES) $(TARGET) *~
21
Makefile
CC = gcc What to name
CFLAGS = -Wall the final prog
LDFLAGS =
OBJFILES = stack.o main.o
TARGET = stacktest
all: $(TARGET)
$(TARGET): $(OBJFILES)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJFILES) $(LDFLAGS)
clean:
rm -f $(OBJFILES) $(TARGET) *~
22
Makefile
CC = gcc TAB
CFLAGS = -Wall not several spaces
LDFLAGS = Sorry…
OBJFILES = stack.o main.o
TARGET = stacktest
all: $(TARGET)
$(TARGET): $(OBJFILES)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJFILES) $(LDFLAGS)
clean:
rm -f $(OBJFILES) $(TARGET) *~
23
To use our Makefile:
• Just type “make”
– It will figure out which .c files need to be
recompiled and turned into .o files
• If the .c file is newer than the .o file or
• the .o file does not exist
– Figures out if the program needs to be re-
linked
• If any of the .o files changed or
• If the program does not exist
24
To use our Makefile:
• Or type “make clean”
– Deletes:
• all the .o files
• all the ~ files (from emacs)
• the program itself
– Leaves:
• .c files
• .h files
• Makefile
25
To use our Makefile:
• make clean
• make
• What happens?
26