Ahmed SALIHI
Ahmed SALIHI

Reputation: 1

what does % mean in a makefile rule

So i know that the pattern rule %.o : %.cpp is for making any file.o from a file.cpp But what I want is different. Let's say I want to make a file1.o from folder1/file1.cpp, and a file2.o from folder2/file2.cpp. What i thought about doing was :

file1=folder1/file1
file2=folder2/file2

%.o: $(%).cpp

But that doesn't work. Do anyone have an idea on how to do that apart from manually doing :

file1=folder1/file1
file2=folder2/file2

file1.o: $(file1).cpp

file2.o: $(file2).cpp

Upvotes: 0

Views: 115

Answers (1)

R Sahu
R Sahu

Reputation: 206567

The meaning of % in GNU Make is documented in the seciton on Defining and Redefining Pattern Rules.

If the target name is foo.o, then % is foo. I don't want to go into any more depth on the meaning of % here.

In your case, the dependencies between the object files and the source files cannot be expressed easily using a single simple rule. I would advise making the rules explicit instead of using a pattern-based rule.

file1.o : folder1/file1.cpp
file2.o : folder2/file2.cpp

You'll need to add the commands to build those targets too.

file1.o : folder1/file1.cpp
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $^

file2.o : folder2/file2.cpp
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $^

Upvotes: 2

Related Questions