0

A part of my Makefile:

CFLAGS = -I ../headers -Wall
EXECUTABLES = testPattern testPatterns

$(EXECUTABLES): %Pattern: %.c pattern.o
      g++ $(CFLAGS) [email protected] pattern.o -o $@

I didn't include the object compilation because it's irrelevant.

The problem I have with this code is that the pattern matches only the first executable because it ends with 'Pattern', the second executable has an additional 's' at the end which kills the script. Is there any way I can make it work without changing the name of the second executable?

Thanks

1 Answer 1

2

There's no point in using a static pattern rule here, since the pattern doesn't appear in the prerequisites list. However, I assume you also wanted to include the .c file as a prerequisite here.

Why do you include the Pattern in the pattern match?

You can just write:

CFLAGS = -I ../headers -Wall
EXECUTABLES = testPattern testPatterns

$(EXECUTABLES): % : %.c pattern.o
        g++ $(CFLAGS) $^ -o $@

ETA It appears a better example that would show the real issues you face would be something like this:

EXECUTABLES = someThing somePattern morePatterns

where you want a static pattern rule that matches the two binaries containing Pattern but not the others.

As I said in my comment below, you cannot do this with a single pattern which means you can't do it in the target part of a static pattern rule.

However, you could do it like this:

$(foreach E,$(EXECUTABLES),$(if $(findstring Pattern,$E),$E)): % : %.c pattern.o
        g++ $(CFLAGS) $^ -o $@

This basically loops through each entry in EXECUTABLES and tests to see if it contains the string Pattern, and if so expands to that string else expands to nothing.

Sign up to request clarification or add additional context in comments.

2 Comments

Hello, sorry I messed up but corrected it already. The file name is in the pre-requistes. What I need a pattern for is because I have a few more executables that I have completely different names and patterns and want to store them all in one variable. I don't want any omitted or false dependencies. I will make new rules for them, but I want them all in one variable.
Your example is not really very good since the prefix test is identical. If you used EXECUTABLES = fooPattern barPatterns it would be a better example. It's not possible to write a single pattern which will match on multiple non-contiguous non-constant parts. For your example above, you cannot write a single pattern which matches both the prefix before Pattern and a suffix after Pattern. You can only have one % per word so you can only match one side or the other.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.