开发者

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:

# Makef开发者_如何学Goile
.PHONY: all

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

all:$(TARGETS)

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


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 $<
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜