3

I am using GNU make. In a Makefile that I got, i see the symbol '%'. e.g. in context of %.c, %.asm , %.o etc.. (Is it a wildcard that will return all the files of that extension)

What does % mean?

What will the below rule do:

%.o:  %.c
  gcc $< -o:$@

thank you,

-AD

andrewdotn
  • 32,721
  • 10
  • 101
  • 130
goldenmean
  • 18,376
  • 54
  • 154
  • 211
  • This question is "what aim are wildcards for?" There was another question phrased "how can I achieve the aim" with answer "with wildcards!" I'd call it a reverse-dupe :-) Here's the question: http://stackoverflow.com/questions/1633527/wildcard-targets-in-a-makefile – P Shved Dec 15 '10 at 15:41
  • I was reading about pattern rules at: gnu.org/software/automake/manual/make/Pattern-Rules.html There I read a statement which said: "A pattern rule looks like an ordinary rule, except that its target contains the character ‘%’ (exactly one of them). " What does exactly one of them mean? – goldenmean Dec 15 '10 at 15:50

1 Answers1

2

The rule:

%.o:  %.c
    gcc $< -o:$@

Will for each c file build an object file using the command below it. So yes, % is like a wildcard, but I tend to think of it as "for each" because that's what happens as a result of the rule. $< means dependent files and $@ means the object to be built. If you did this:

fred.o: fred.c
    gcc $< -o:$@

Here $@=fred.o and $<=fred.c Likewise

fred.o: fred.c george.c
    gcc $< -o:$@

gives $@=fred.o and $<=fred.c george.c.

Edit: from the commands I shall expand on this.

% will match everything matching that around it. So %.c means all the .c files in the current directory. asm/%.asm means all the files in the subdir asm with the .asm extension. It acts like a token, so whatever is found will be used whenever you next use % i.e. in the label.

So, the rule:

objs/asm/%.o: arch/%.S
     nasm -felf64 $< -o $@

Given arch/hello.S, arch/bye.S, arch/somethingelse.S will create obj/asm/hello.o, obj/asm/bye.S, obj/asm/somethingelse.o.

  • I was reading about pattern rules at: http://www.gnu.org/software/automake/manual/make/Pattern-Rules.html There I read a statement which said: "A pattern rule looks like an ordinary rule, except that its target contains the character ‘%’ (exactly one of them). " What does exactly one of them mean?. Also I had a pattern rule like: %.obj: isp/%.asm $(ASM) $< -o:$@ but somehow it was not picking all the asm files for assembling. I added one assembly file later to that folder and it was not getting assembled. Had to manually add the build rule for that file. – goldenmean Dec 15 '10 at 15:50
  • It means that whatever % represents will be used everywhere % is mentioned. You can't match two things at once. That rule will match everything in isp/somename.asm and create somename.o. Try ls isp/*.asm. That's what isp/%.asm will match. Then it will take the %, put a .o on the end and that becomes the name of the rule (the thing to be made). –  Dec 15 '10 at 15:55
  • Ok , so if %.asm means for each asm file, convert each of those to *.obj. So when I add one more asm file to the existing files in the folder, but it was not getting assembled? What could be the reason. Sorry i am bit confused? – goldenmean Dec 15 '10 at 16:01