0

I'm trying to add an implicit rule to a Makefile, and I'd like it to handle compilation of .c and .cpp files the same way. The rule I have for .c files is as follows:

%.obj: %.c
    cl /c $(CFLAGS) $<

What I'd like, though, is for the right side of the pattern to match .c OR .cpp, depending on which file is available. Is this possible? I've perused the Make manual, but haven't found what I'm looking for. Any help would be greatly appreciated.

1 Answer 1

5

Just use two rules which is equivalent to "or" in the make language:

%.obj: %.c
    cl /c $(CFLAGS) $<

%.obj: %.cpp
    cl /c $(CFLAGS) $<

At the end these are two different source languages and you may well end up desiring different flags.

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

2 Comments

Ah, fantastic, thanks! I had misinterpreted something from the manual as meaning that the above would override the previous rule rather than function as an OR, and thus hadn't even tried it. Much appreciated!
Come to think about it is more like a switch or simple alternates -- these are after all different source files.

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.