Pregunta

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 ?

¿Fue útil?

Solución

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.

Otros consejos

CC=gcc
DEBUGFLAGS=-g
RELEASEFLAGS=-O
debug/%.o : %.c
    $(CC) $(DEBUGFLAGS) -c $< -o $@

release/%.o : %.c
    $(CC) $(RELEASEFLAGS) -c $< -o $@
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top