user2808264 user2808264 - 1 year ago 134
C Question

Makefile Explanation

I am new to makefiles and trying to understand some code:

$(CC) $^ -c -Wall
$(CC) -MM $*.c > $*.dep
$(OBJECTDUMP) -d -M intel -S $@
@mv -f $*.dep $*.dep.tmp
@sed -e 's|.*:|$*.o:|' < $*.dep.tmp > $*.dep
@sed -e 's/.*://' -e 's/\\$$//' < $*.dep.tmp | fmt -1 | \
sed -e 's/^ *//' -e 's/$$/:/' >> $*.dep
@rm -f $*.dep.tmp

Can someone please explain what the last 5 lines of code are doing?

Answer Source

$* corresponds roughly to the % in the first line.

  • The @mv line moves the old basename.dep file to basename.dep.tmp
  • The first @sed line edits what's in the basename.dep.tmp, replacing anything up to a colon with basename.o: (because the $* is expanded by make, not the shell).
  • The second @sed line and the following line do some more editing — appending another variant of the basename.dep.tmp file to the end of basename.dep.
  • The @rm line removes the temporary basename.dep.tmp file.

A more thorough analysis of the second sed sequence requires more detailed knowledge of what's in the .dep file and exactly what fmt -1 does for you.

However, it seems that the goal is to update the dependencies that apply to the source file, based on the information from the compiler plus massaging it in a way to suit the programmer. The why is not clear to me.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download