remove ../ from path

I have object files coming in with paths that might look like this:

 '../../src/foo/bar.c'

I'd like them to be output to

 'build/src/foo/bar.o'

Currently using:

 COBJS      :=  $(notdir $(CFILES))
 COBJS      :=  $(patsubst %,$(BUILD)%.o,$(COBJS))

I can achieve

 'build/bar.o'

This is problematic if any two library/project contains the same class name.

So the question is, how can one remove multiple '../' from a path in Make. I've attempted the obvious and naive approaches with no results.

Update, the following will match exactly ../../ and replace it with the rest. This is perfect except that it is specific to ../../. Just need to make it match any number of ../../

 COBJS      :=  $(CFILES:../../%=%)

Update,

SOLVED, just three reputation shy of posting my own answer.

 COBJS      :=  $(subst ../,,$(CFILES))

As posted in my original question and I forgot to eventually answer.

The solution for this and likely many other Make string replacement is as follows:

COBJS      :=  $(subst ../,,$(CFILES))

'subst' takes 3 parameters. $toMatch, $replaceWith, $string.

In this case $(CFILES) is the list of all .c files to compile. I replace '../' with nothing.


尝试abspath。

COBJS := $(foreach file,${CFILES},$(abspath ${file}))

A simple solution to your problem can be as follows. Consider a simple Makefile

path_to_remove := batch1/test2
path_to_add := earth/planet

my_paths := 
batch1/test2/p/a3.c 
batch1/test2/p/a2.c 
batch1/test2/p/a1.c  
batch1/test2/q/b3.c 
batch1/test2/q/b2.c 
batch1/test2/q/b1.c

$(info [Original] [$(my_paths)])
my_paths := $(my_paths:$(path_to_remove)/%=$(path_to_add)/%)
$(info [New Changed] [$(my_paths)])

all:
    @echo "Hi Universe"

When you run make command.

make

You will get ouput as:

[Original] [batch1/test2/p/a3.c batch1/test2/p/a2.c batch1/test2/p/a1.c batch1/test2/q/b3.c batch1/test2/q/b2.c batch1/test2/q/b1.c]
[New Changed] [earth/planet/p/a3.c earth/planet/p/a2.c earth/planet/p/a1.c earth/planet/q/b3.c earth/planet/q/b2.c earth/planet/q/b1.c]
链接地址: http://www.djcxy.com/p/10546.html

上一篇: 在哪里建立新的域实体? 控制器,存储库或映射器?

下一篇: 从路径中删除../