I'm trying to write a custom Makefile in which I collect all source files to compile against the main. First I collect their names:
# other sources to compile against (without extensions, separated by spaces)
DEPENDENT_FILES = greet farewell
Then I'd like to append .o
to each word. In a shell, I can do this in many ways, e.g.
echo '$DEPENDENT_FILES' | sed 's/\>/\.o/g' # but I know, it's UNIX dependant
# or ...
echo '$DEPENDENT_FILES' | sed 's/[^ ]*/&\.o/g'
both result in greet.o farewell.o
on the terminal, but fail (resulting in empty strings) during the make
process.
I write them as
DEPENDENCIES := $(echo '$DEPENDENT_FILES' | sed 's/\>/\.o/g')
# or ...
DEPENDENCIES := $(echo '$DEPENDENT_FILES' | sed 's/[^ ]*/&\.o/g')
# but they result in
$(info DEPENDENCIES is $(DEPENDENCIES)) # "DEPENDENCIES is "
I don't know what I'm doing wrong here, I suspect it could be something related to echos or pipes in Makefiles but I can't figure it out.
Please, I'd prefer answers related to working sed and echo solutions, without proposing different approaches: it's not the first time that I'm facing problems with echo and pipes in Makefiles and I still don't know what is wrong in my approach.
/^$/!
can be written as prefix in the sed rule to avoid that an empty$DEPENDENT_FILES
is translated to.o
(e.g.sed '/^$/!s/[^ ]*/&\.o/g')
– Maik93 Dec 29 '20 at 9:50addsuffix
function. E. g.DEPENDENCIES = $(addsuffix .o, $(DEPENDENT_FILES))
. – phg Dec 29 '20 at 10:08sed ...
part withcat
, see if the pipe works. If it does, useecho
instead ofsed
and no pipes to see what quotation make processes. – dirkt Dec 29 '20 at 10:24