0% found this document useful (0 votes)
72 views26 pages

Lecture Makefile

The document discusses breaking a program into multiple files for organization and easier compilation. It recommends separating code into main.c for the main function, stack.c for stack implementation code, and stack.h for stack declarations. A Makefile is provided to automate compiling the files into an executable called stacktest. Typing "make" will compile updated files, while "make clean" deletes object files and executables before recompiling.

Uploaded by

Josef Cavalir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views26 pages

Lecture Makefile

The document discusses breaking a program into multiple files for organization and easier compilation. It recommends separating code into main.c for the main function, stack.c for stack implementation code, and stack.h for stack declarations. A Makefile is provided to automate compiling the files into an executable called stacktest. Typing "make" will compile updated files, while "make clean" deletes object files and executables before recompiling.

Uploaded by

Josef Cavalir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Makefiles, and .

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;

void push(int number, stack **stk_ptr);


int pop(stack **stk_ptr);

• 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"

• Why include stack.h?


• Note the “” instead of <>
– <> means “include from the system libraries”
• For predefined .h files
– “” means “include from THIS directory”
• For your OWN .h files

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"

• Why include stack.h this time?

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

• Just use the one I give you, and only


modify the top parts

• Makefiles could be a class on their own…

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

You might also like