Using patsubst to convert one path into another

In order to make the directory strcuture of my project a bit more organized I want to put sourcefiles for the same modules into seperate directory. All objectfiles should be put into obj directory. I don't really want to replicate the source tree in the obj directory as well, so I'm looking for a way to remove the directory part from a source file to make it into the obj path.

So this is the current version, which would require the obj directory to be of the same layout as the source directory.

OBJDIR:= obj
SRCDIR:= src

SRC := propertyfile/propertyfile.cpp otheritems/file.cpp otheritmes/subtree/bla.cpp
OBJ := $(patsubst %.cpp, $(OBJDIR)/%.o, $(SRC))

To make it more clear: My sources reside in

src/propertyfile/*
src/otheritems/*
src/otheritmes/subtree/*

and so on.

The objfiles should all be in obj without the intermediate directories. With the above patsubst, it simply replaces the src part to obj making ie src/propertyfile/propertyfile.cpp into obj/propertyfile/propertyfile.o but what I would like to have is obj/propertyfile.o

A pointer to a good explanation on how these patterns work and what parameters it accepts would also be helpfull.

UPDATE:

So far I made a little progress by using the following line:

OBJ :=      $(addprefix obj/,$(notdir $(SRC:.cpp=.o)))

However now the build rule doesn't work anymore because the names don't match, so I would need a pattern that works for the build rule as well.


One way to achieve this would be using vpath.

Another way, something like:

OBJ := ${SRC:%.cpp=${OBJDIR}/%.o}
链接地址: http://www.djcxy.com/p/61442.html

上一篇: makefile:4:***缺少分隔符。 停止

下一篇: 使用patsubst将一个路径转换为另一个路径