Andna
Andna

Reputation: 6689

Make target specific variables

I am writing small makefile, and I have a problem with target specific variables, I have this piece of code:

FILE_SOURCE := pliki.c wczytaj_plik.c wypisz_plik.c
CONSOLE_SOURCE := wczytaj_konsola.c wypisz_konsola.c
OTHER_SOURCE := suma.c roznica.c iloczyn.c macierz.c
HEADERS := suma.h roznica.h iloczyn.h wypisz.h wczytaj.h macierz.h
DEFINE_OPT = 
NAME=macierze
FILE_OBJECTS := $(FILE_SOURCE:.c=.o)
CONSOLE_OBJECTS := $(CONSOLE_SOURCE:.c=.o)
OTHER_OBJECTS := $(OTHER_SOURCE:.c=.o)
finput: HEADERS+=pliki.h
finput: DEFINE_OPT+=-D WEWY_PLIKI
finput: OTHER_OBJECTS+=$(FILE_OBJECTS)

finput cinput: debug $(NAME)

$(NAME): $(OTHER_OBJECTS) main.o
    @echo $^
    gcc $(CFLAGS) -o $(NAME) $^  

debug:
    @echo $(OTHER_OBJECTS)

this is a piece that is relevant, when I invoke

make finput

in target debug I get all the .o files but

@echo $^

only produces

suma.o roznica.o iloczyn.o macierz.o main.o

so it is like FILE_OBJECTS were not added, but in gnu make manual:

There is one more special feature of target-specific variables: when you define a   target-specific variable that variable value is also in effect for all prerequisites of this   target, and all their prerequisites, etc. (unless those prerequisites override that variable  with their own target-specific variable value).

So it is a bit weird that $(OTHER_OBJECTS) in $(NAME) don't include $(FILE_OBJECTS), how can I fix this problem?

Upvotes: 1

Views: 2060

Answers (1)

eriktous
eriktous

Reputation: 6659

If you look a couple lines up in the same section of the GNU make manual you quoted from, you will find the following.

As with automatic variables, these values are only available within the context of a target's recipe

This means the target specific value of OTHER_OBJECTS is not available in the prerequisites. (I haven't tried, but perhaps you can use the same workaround as with automatic variables, namely secondary expansion.)

Upvotes: 2

Related Questions