con-f-use
con-f-use

Reputation: 4028

How can I get make to put the binaries in a different location?

Short and easy question, but I seem to have a writer's block here:

Suppose I have a source code file in the same directory as the makefile I use to build the program:

confus@confusion:~/prog$ ls
binaries  includes  main.c  Makefile

How do I get make to put the binaries for my main.c in the binaries dir? Afterwards on a second run make should see if the binary file there is up to date (and don't compile it again) just like normal.

My thought was something like this:

# Makefile
.PHONY: all

SOURCES := $(wildcard *.c)
TARGETS := $(subst %.c,binaries/%.o,$(SOURCES))

all:$(TARGETS)

$(TARGETS):$(SOURCES)
    ./compile "$(subst .o,.c,$(@F))" -o "$@"

Upvotes: 2

Views: 1523

Answers (1)

nhed
nhed

Reputation: 6001

Don't say all targets depend on all sources, instead have a pattern rule

binaries/%.o: %.c
    ./compile ... -o $@ -c $<

you may also need to use a vpath

Revised: You also had a problem with your subst ... this test worked (just for compiling individual .o files, you still need to link them, which would be a very simple rule)

# Makefile
.PHONY: all

SOURCES := $(wildcard *.c)
TARGETS := $(patsubst %.c,binaries/%.o,$(SOURCES))

all:$(TARGETS)

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

Upvotes: 2

Related Questions