|
| 1 | +#vim: noexpandtab: ts=8: softtabstop=8 |
| 2 | + |
| 3 | +# names and places |
| 4 | +SRC_DIR = src |
| 5 | +INC_DIR = src |
| 6 | +TARGET = testing_C |
| 7 | +BLD_DIR = build |
| 8 | +#tools |
| 9 | +CC = gcc |
| 10 | +#CPPFLAGS= -I $(SRC_DIR) |
| 11 | +CPPFLAGS+= --save-temps |
| 12 | +CFLAGS = -g -Wall |
| 13 | +CFLAGS = -Os -g -Wall |
| 14 | +LDFLAGS = -g |
| 15 | +LDFLAGS = -g -Os |
| 16 | +# ----- automated filenames ----- |
| 17 | +C_SRCS = $(wildcard $(SRC_DIR)/*.c) |
| 18 | +INC_FILES = $(wildcard $(SRC_DIR)/*.h) |
| 19 | +OBJ_FILES = $(C_SRCS:$(SRC_DIR)/%.c=$(BLD_DIR)/%.o) |
| 20 | + |
| 21 | +#targets - elements |
| 22 | +# the colours are bonus ("\033[....]") |
| 23 | +$(BLD_DIR)/%.o: $(SRC_DIR)/%.c $(INC_FILES) |
| 24 | + @echo "\033[07;36m" |
| 25 | + @echo $< generates $@ |
| 26 | + $(CC) -c $(CFLAGS) $< -o $@ |
| 27 | + @echo "\033[00m" |
| 28 | + |
| 29 | + |
| 30 | +$(TARGET): $(OBJ_FILES) |
| 31 | + @echo "\033[01;33m" |
| 32 | + @echo building $@ from $(OBJ_FILES) |
| 33 | + @echo "\033[00m" |
| 34 | + $(CC) $(LDFLAGS) -o $@ $(OBJ_FILES) |
| 35 | + |
| 36 | +#not necessary, but really useful to get rid of stray files ans errors |
| 37 | +clean: |
| 38 | + rm -rf $(BLD_DIR)/* |
| 39 | + rm $(TARGET) |
| 40 | + |
| 41 | +#=======================# |
| 42 | +# what follows is bonus to make life cool |
| 43 | + |
| 44 | +# the target is not a file <> "PHONY" |
| 45 | +.PHONY: all run clean listfiles test hello |
| 46 | + |
| 47 | +check: $(OBJ_FILES) $(TARGET) |
| 48 | + |
| 49 | +test: $(C_SRCS) $(OBJ_FILES) |
| 50 | + @echo "includes \t:" $(INC_FILES) |
| 51 | + @echo "sources \t:" $(C_SRCS) |
| 52 | + @echo "objects \t:" $(OBJ_FILES) |
| 53 | + @echo "test \t\t:" $(C_SRCS:$(SRC_DIR)/%.c=$(BLD_DIR)/%.o) |
| 54 | + |
| 55 | +hello: |
| 56 | + @echo "\n\tHey Dude, nice to see you!\n" |
| 57 | + |
| 58 | +all: run listfiles hello |
| 59 | + |
| 60 | + |
| 61 | +run: $(TARGET) |
| 62 | + @echo "\033[01;32;44m" |
| 63 | + @echo "-+-+-+-" |
| 64 | + ./$(TARGET) |
| 65 | + @echo "-+-+-+-" |
| 66 | + @echo "\033[00m" |
| 67 | + |
| 68 | +listfiles: |
| 69 | + @echo current dir: |
| 70 | + @ls |
| 71 | + @echo "\033[01;32m"sources"\033[00m" |
| 72 | + @ls -lAh $(SRC_DIR) |
| 73 | + @echo "\033[01;32m"objects"\033[00m" |
| 74 | + @ls -lAh $(BLD_DIR) |
0 commit comments