views:

41

answers:

4

Given the line:

program_OBJS := ${program_SRCS:.cpp=.o}

I would like to append .o to each filename instead of replacing .cpp with .o.

How do I do that?

+1  A: 

Just a guess program_OBJS := ${program_SRCS:.cpp=.cpp.o}

ur
+1  A: 

To just append something to a list of space separated items you can use:

program_OBJS := $(foreach program,$(program_SRCS),$(program).o)

To use the substitution method (like you show in your question):

program_OBJS := $(program_SRCS:.cpp=.cpp.o)

but for that the list must contain the .cpp suffices, or the substitutions will not occur.

Veger
Why does the space matter?
Robert S. Barnes
I thought `$(foreach)` literally expended the third argument and concatenated the results. I just tested it and it automatically adds a space if omitted, so it is not required. (I will remove the note from my answer and update my own Makefiles... ;) )
Veger
+2  A: 

there is also addsuffix function

Basilevs
+1 Does this work in other versions of make, or just gnu make?
Robert S. Barnes
+1  A: 

Shorter alternative, using a pattern substitution: program_OBJS := ${program_SRCS:%=%.o}

slowdog