21

How do I add comments (with echo) in a Makefile so that they're printed when ran?

6 Answers6

32

You should use

target:
     @echo "Building!"

Note the @, which tells Make not to display the command itself. Without this the output would look like:

echo "Building!"
Building!
Alnitak
  • 334,560
  • 70
  • 407
  • 495
9

Or, since Make just pushes whatever is in a rule to bash, you could just use a pound to have bash treat it as a comment.

Rule:  Dependencies
    # Your Comment
    Command

Will output

$ make Rule
    # Your Comment
    Command
FrankieTheKneeMan
  • 6,645
  • 2
  • 26
  • 37
  • 4
    and how can you do the opposite? put a comment in a makefile that doesn't get printed? – knocte Aug 21 '13 at 14:39
  • 4
    ended up asking it: http://stackoverflow.com/questions/18360776/how-to-not-print-in-the-output-a-comment-in-a-makefile/18363477?noredirect=1#18363477 – knocte Aug 21 '13 at 17:08
  • 2
    @knocte You can use an `@` before `#` – mems Oct 13 '16 at 13:23
  • 1
    This should be the accepted answer IMO, much more readable and produces the exact same result as `@echo "…"` – sylbru Jan 11 '17 at 16:02
2

Since a makefile mostly contains commands to be run when building specific targets, I'd say you use just that: echo.

Joey
  • 344,408
  • 85
  • 689
  • 683
2
all :
    echo "Building!"
    $(CC) $(OBJECTS) $(LPATH) $(LIBS) -o $(PROGRAM)
BenB
  • 10,300
  • 7
  • 32
  • 30
2

Visual C++ nmake has the !message text... preprocessing directive. I have not used GNU make, so I don't if it has it as weel, but quick search shows it has the $(info text...) function.

And inside command blocks you can use echo.

Franci Penov
  • 74,861
  • 18
  • 132
  • 169
1

An imperfect but simple workaround is to add your comments outside the target:

Rule:  Dependencies
    Command
# Your Comment
Christoph Thiede
  • 707
  • 10
  • 16