Search code examples
cmakefilegbdk

Makefile output into subfolder


I'm working on setting up a project for a GameBoy game, I want to have the files output into a subfolder of the project rather than the root. Adding the foldername in front of the filenames doesn't seem to work nor do any of the suggestions I've found online.

This is what I currently have for the makefile.

CC  = ../../../bin/lcc -Wa-l -Wl-m -DUSE_SFR_FOR_REG


BINS    = new.gb

all:    $(BINS)

%.o:    %.c
    $(CC) -c -o $@ $<

%.s:    %.c
    $(CC) -S -o $@ $<

%.o:    %.s
    $(CC) -c -o $@ $<

%.gb:   %.o
    $(CC) -o $@ $<

clean:
    rm -f *.o *.lst *.map *.gb

# Link file, and write 0x80 at position 0x143 in header
new.gb: new.o
    $(CC) -Wl-yp0x143=0x80 -o new.gb new.o

Thanks.


Solution

  • Since you only have a single source file you don't need to compile the .o file separately, just reuse make's one-shot rule

    CC       := ../../../bin/lcc
    CPPFLAGS := -DUSE_SFR_FOR_REG
    CFLAGS   := -Wa-l
    LDFLAGS  := -Wl-m -Wl-yp0x143=0x80
    
    subdir/new.gb: new.c
        $(LINK.c) $^ $(LDLIBS) -o $@
    
    .PHONY: clean
    clean:
        $(RM) $(addprefix subdir/new,.lst .map .gb)