Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

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)])
    @echo "Hi Universe"

When you run make command.

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]
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.