51

I have a makefile that is like this:

install:
    @somecommand

    #some explanation for next command
    @lastcommand

What happens is that the comment #some explanation for next command is being printed when I execute make install. How can I make a comment in a makefile that doesn't get printed? Maybe I'm looking for the unix equivalent for the windowsy echo off?

(Effectively, the opposite of this question.)

Community
  • 1
  • 1
knocte
  • 16,941
  • 11
  • 79
  • 125

2 Answers2

85

Don't indent the comment — when the line starts with a tab, it is a command that is executed by the shell (and the shell treats the comment as a comment).

Proof of concept (ss.mk):

all:
    echo "This is the first command"
    # This comment is echoed

# This comment is not echoed
    echo "This is the second command"

Sample output:

$ make -f ss.mk
echo "This is the first command"
This is the first command
# This comment is echoed
echo "This is the second command"
This is the second command
$
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 20
    An indented comment prefixed with an `@` will also not be shown, if you *really* want to indent it :-) – cmbuckley Apr 30 '14 at 23:29
  • 11
    @cbuckley: That is true, but it is a very heavy-weight way of writing a comment because `make` will fork and exec a shell which will scan the comment, deduce that it is a comment, and then exit successfully…unless `make` optimizes for that, which it probably won't. – Jonathan Leffler May 01 '14 at 01:58
  • Yeah... previously my comment ended with something to the effect of "...but that's probably being ridiculous." Not sure why I removed it! – cmbuckley May 01 '14 at 18:10
  • The problem with not-indenting-it is that you can't assurely know the tab size of the editor, and so can't align things (like command-line switches) pretty way. – Des Nerger Aug 07 '23 at 13:56
3

I try this and it works perfectly:

test:
    @echo "test 1"
    @# echo "test 2"
  • output:
$ make test

test 1
  • handle error:
    if you want to want to assure execute all lines, shall use - at the beginning of the line that perhaps can stop your make file.
test:
    @echo test1
    -dkfhfwefkwef
    @echo test2
    @-jddhwehjwejfw
    @echo test3
$ make test

test1
dkfhfwefkwef
make: dkfhfwefkwef: No such file or directory
make: [Makefile:3: test] Error 127 (ignored)
test2
make: jddhwehjwejfw: No such file or directory
make: [Makefile:5: test] Error 127 (ignored)
test3
nextloop
  • 166
  • 9