Pregunta

I'm having a file structre like

/ 
 |- Makefile
 |- libs
    |- lib1
    |- lib2
 |- src
    |- file.cpp

I have one top-level Makefile which includes a Rules.mk file within every subdirectory. The src file rule depends on lib1 and lib2, which get built by the makefile as well.

now I have a target like:

# predefined rule
COMP := gcc -o $@ $< $(FLAGS)

file.o: OBJS := libs/lib1/lib1.o libs/lib2/lib2.o
file.o: file.cpp $(OBJS)
    $(COMP) $(OBJS)

when building the stuff (default target is file.o) it complains about the libraries not being there. When I add an echo to the file.o task and print out $(OBJS) it prints out all the libs I've assigned to it, but make doesn't trigger building them.

When I replace the variable $(OBJS) in the dependencies with the filenames, it triggers the builds and then obviously finds the required libraries.

I thought it might have something to do with the .SECONDARYEXPANSION, but it shouldn't as I'm not using any delayed variables.

Thanks in advance!

regards Chris

PS: the project is on Github anyway, the following file contains the Rule I'm talking about: https://github.com/janhancic/CHIC/blob/242c0ceee558c2eb4b21fe73e94a9ab5f3fe0b49/src/Rules.mk

¿Fue útil?

Solución

You can't do this. The documentation for GNU make is very clear: target-specific variables are available ONLY inside the recipe. They are not available in the target or prerequisite lists. In the prerequisite list make will expand $(OBJS) the value of the global variable OBJS, or the empty string if that doesn't exist; NOT to the target-specific variable value.

However, I don't understand why you write this:

file.o: OBJS := libs/lib1/lib1.o libs/lib2/lib2.o

when you could just write this:

file.o: libs/lib1/lib1.o libs/lib2/lib2.o

and have it work fine?

Otros consejos

In addition to what MadScientist already said,

COMP := gcc -o $@ $< $(FLAGS)

uses immediate assignment, which evaluates the expression on the right immediately, the result being essentially COMP := gcc -o $(FLAGS). Use the deferred assignment instead:

COMP = gcc -o $@ $< $(FLAGS)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top