Object file directory per compiler option combinat

2019-08-24 18:49发布

I was reading gnu make section 10.5.4 "How patterns match" and it does not sound like I can do what I want.

I want to setup a directory structure where my source code is in one directory, and there are sub-directories to hold object files. One sub-directory for each build configuration. So I might have these files

a.c
debug/a.o # compiled with -g
release/a.o # compiled with -O

So I would like to make rules like this

debug/%.o : %.c
    gcc -c -g %.c -o $@

release/%.o : %.c
    gcc -c -O %.c -o $@

But section 10.5.4 tells me a match on "debug/a.o" will make the stem be "debug/a" so gnu make will look for the source file at "debug/a.c" which is not what I want.

Is there a way to get GNU make to help me ?

2条回答
女痞
2楼-- · 2019-08-24 18:49

Your makefile will work as written.

From that section of the manual:

When the target pattern does not contain a slash (and it usually does not), directory names in the file names are removed from the file name before it is compared with the target prefix and suffix. After the comparison of the file name to the target pattern, the directory names, along with the slash that ends them, are added on to the prerequisite file names generated from the pattern rule's prerequisite patterns... [bold added]

Your target patterns do contain slashes.

Try it if you don't believe me.

EDIT:

Correction: in the commands you should use $< rather than %.c.

查看更多
做自己的国王
3楼-- · 2019-08-24 19:12
CC=gcc
DEBUGFLAGS=-g
RELEASEFLAGS=-O
debug/%.o : %.c
    $(CC) $(DEBUGFLAGS) -c $< -o $@

release/%.o : %.c
    $(CC) $(RELEASEFLAGS) -c $< -o $@
查看更多
登录 后发表回答