tags:

views:

69

answers:

2

If I erase the gcc lines from this file shouldn't it take compilation as the implicit rule? Why isn't it allowing me to run the program ./calc with that makefile configuration?

Makefile:

all: calc 

clean:  
    rm -rf calc arit.o calc.o

calc:   calc.o arit.o
    #gcc -o calc calc.o arit.o

calc.o: calc.c arit.h
    #gcc -c calc.c 

arit.o: arit.c arit.h
    #gcc -c arit.c
+5  A: 

Because the comment is indented by a tab stop, it is treated as a command (and executed by the shell, which treats it as a comment).

If the '#' symbols were in column 1, then they would be pure (make) comments.

Jonathan Leffler
+1, Checked it and you are right.
codaddict
+2  A: 

Further to Jonathan Leffler's answer, the following minimal GNUMakefile should do all compilation and linking through implicit rules only:

calc: calc.o arit.o
arit.o: arit.c arit.h
calc.o: calc.c arit.h
clean:
    rm -rf calc arit.o calc.o
Jack Kelly
Interesting: I was not aware that you could omit the link line commands with GNU Make. There are some versions of Make where that would not be possible - for instance, Solaris make, and I believe POSIX make does not guarantee that.
Jonathan Leffler
@Jonathan Leffler: http://www.gnu.org/software/make/manual/make.html#Catalogue-of-Rules does not list it as a non-POSIX feature (look for "Linking a single object file"). I built `make` from the heirloom project and tested that and it also links both objects, so I'd guess that it's reasonably portable.
Jack Kelly
Define 'reasonably portable'. It does not work on Solaris 10 make. It does not work on HP-UX 11.11 make. It does not work on AIX 5.3 make. That leaves Linux and MacOS X, which both use GNU make, as the systems where it does work. **YMMV**. I note that the manual says "This rule does the right thing for a simple program with only one source file. It will also do the right thing if there are multiple object files (presumably coming from various other source files), one of which has a name matching that of the executable file." That is no longer 'linking a single object file'.
Jonathan Leffler
FWIW: On HP-UX and Solaris, make doesn't try to run a link command; on AIX, make runs a single-object link command ('`cc -O calc.c -o calc`').
Jonathan Leffler
@Jonathan Leffler: Touché. My instinct was to reach for heirloom make as some representative of strict standards compliance, which turned out to be incorrect.
Jack Kelly