45

Is there a way to remove a prefix from a string (a pathname in my case) in make?

As an example, suppose I had the string:

FILES = a/b/c.d a/b/e.f

I want to remove the a/, and be left with b/c.d b/e.f

I have tried using various combinations of dir, notdir and basename from the GNU make manual, but none seem to provide a nice solution.

$(dir $(FILE))      # a/b a/b
$(notdir $(FILE))   # c.d e.f
$(basename $(FILE)) # a/b/c a/b/e

The only way I've found to do this so far is:

$( join $(basename $(dir $(FILE))), $(notdir $(FILE)) )

Which is really ugly and long-winded. What I really need is some kind of $(removeprefix ...) function. Assuming that I know the prefix (a/) to be removed, can this be done with some kind of string manipulation?

2 Answers 2

91

You can strip off a leading a/ with

$(FILE:a/%=%)

See the text substitution function reference for more options & details.

0
25

Since you say GNU make, why not just:

$(FILE:a/%=%)

?

1
  • 8
    Guessing that was since there's an identical answer above... but which was apparently posted in the exact same minute! Unlucky :/ Mar 29, 2017 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.