Using lists as pattern rules in a Makefile
Posted: Tue Oct 16, 2012 9:55 am
I want my OS's source code to be in the directory src and to be built into build/obj and build/bin. So I started writing the Makefile:
Then I have to write the rules. I wanna make them like this:
And that doesn't work. I can use eval with foreach to write it as a pattern rule (%.o: %.c) for each source directory (right now src has no subdirectories, but it will). However, that seems a bit ugly for me. Is there a cleaner way?
Code: Select all
CC = i586-elf-gcc
CFLAGS = -Wall -Wextra -nostdlib -fno-builtin -nostartfiles -nodefaultlibs
LD = i586-elf-ld
ASM = nasm
ASMFLAGS = -f elf
SRCDIRS = src #no subdirectory for now
SRC_C := $(wildcard $(addsuffix *.c, $(SRCDIRS))) #c
SRC_S := $(wildcard $(addsuffix *.s, $(SRCDIRS))) #asm
SRCFILES = $(SRC_C) $(SRC_S)
HEADERS := $(wildcard $(addsuffix *.h, $(SRCDIRS)))
OBJDIR = build/obj
OBJ_C := $(addprefix $(OBJDIR)/, $(addsuffix .o, $(notdir $(SRC_C))))
OBJ_S := $(addprefix $(OBJDIR)/, $(addsuffix .o, $(notdir $(SRC_S))))
OBJFILES = $(OBJ_C) $(OBJ_S)
BUILD_DIR = build/bin
Code: Select all
$(OBJ_C): $(SRC_C)
compile command here, as a foreach