CS 330 Lecture 15 – Makefiles, For Real
Agenda
- what ?s
- the need for separate compilation
- the way to manage separate compilation: make
- polymorphism in C: unions
TODO
- Write a midterm exam question on something you read or something discussed in lecture. Turn it in on a quarter sheet. Some portion of the upcoming midterm will be comprised of your questions. In addition to turning them in, you are encouraged to post them in the comments. Aim for conceptual or at least interesting questions. Bad examples include “How big is an int in Java?”, “What year was C standardized?”, and “What utility dumps a file to the console?” Better examples include “Write a line of code that demonstrates why C is weakly typed,” “What Ruby regex would match strings of bits?”, “The cut utility takes option -f <field-number> and -d <delimiter>. Suppose you’ve got text in the following form… Write a shell command that extracts the 2nd, comma-separated field from the file and sorts the results. Use pipe redirection in your answer.”
- Browse http://bits.stephan-brumme.com and http://graphics.stanford.edu/~seander/bithacks.html. No quarter sheet on this material.
Code
stack.h
#ifndef STACK_H
#define STACK_H
struct stack_t {
int size;
int capacity;
int *items;
};
typedef struct stack_t stack_t;
stack_t *make_stack(int capacity);
void stack_push(stack_t *stack, int item);
int stack_pop(stack_t *stack);
void stack_free(stack_t *stack);
#endif
stack.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "stack.h"
#define EZMALLOC(type, n) \
(type *) malloc((n) * sizeof(type))
stack_t *make_stack(int capacity) {
stack_t *stack = EZMALLOC(stack_t, 1);
stack->size = 0;
stack->capacity = capacity;
stack->items = EZMALLOC(int, stack->capacity);
}
void stack_push(stack_t *stack, int item) {
if (stack->size >= stack->capacity) {
fprintf(stderr, "Stack full, you foo bag.\n");
exit(1);
}
stack->items[stack->size] = item;
++stack->size;
}
int stack_pop(stack_t *stack) {
if (stack->size == 0) {
fprintf(stderr, "Stack empty, you foo bag.\n");
exit(1);
}
int item = stack->items[stack->size - 1];
--stack->size;
return item;
}
void stack_free(stack_t *stack) {
free(stack->items);
free(stack);
}
makefile
Note to copy and pasters: makefiles rules need to be indented with real tabs, not spaces.
CFLAGS = -std=c99 -g
all: calc
stack.o: stack.c stack.h
gcc $(CFLAGS) -c stack.c
calc: calc.c stack.o
gcc $(CFLAGS) -o calc calc.c stack.o
test: calc
valgrind --leak-check=full ./calc
# 2 7 /
clean:
rm -f *.o calc ./a.out
calc.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"
int main(int argc, char **argv) {
stack_t *stack = make_stack(10);
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "+") == 0 ||
strcmp(argv[i], "-") == 0 ||
strcmp(argv[i], "*") == 0 ||
strcmp(argv[i], "/") == 0) {
int a = stack_pop(stack);
int b = stack_pop(stack);
if (argv[i][0] == '+') {
stack_push(stack, a + b);
} else if (argv[i][0] == '-') {
stack_push(stack, b - a);
} else if (argv[i][0] == '*') {
stack_push(stack, a * b);
} else if (argv[i][0] == '/') {
stack_push(stack, b / a);
}
} else {
stack_push(stack, atoi(argv[i]));
}
// if argv[i] is operator
// pop
// pop
// eval
// push
// else
// push
}
printf("stack_pop(stack): %d\n", stack_pop(stack));
stack_free(stack);
return 0;
}
Haiku
Robots water plants
School teachers raise our children
Programs build programs
School teachers raise our children
Programs build programs