144

Is there a way to comment out multiple lines in makefiles like as in C syntax /* */ ?

Teun Zengerink
  • 4,277
  • 5
  • 30
  • 32
Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208

6 Answers6

191

No, there is nothing like C-style /* */ comments in makefiles. As somebody else suggested, you can make a multi-line comment by using line continuations. For example:

# This is the first line of a comment \
and this is still part of the comment \
as is this, since I keep ending each line \
with a backslash character

However, I imagine that you are probably looking to temporarily comment out a chunk of your makefile for debugging reasons, and adding a backslash on every line is not really practical. If you are using GNU make, I suggest you use the ifeq directive with a deliberately false expression. For example:

ifeq ("x","y")
# here's all your 'commented' makefile content...
endif
starball
  • 20,030
  • 7
  • 43
  • 238
Eric Melski
  • 16,432
  • 3
  • 38
  • 52
17

I believe the answer is no. The only comment styling I can find is # for each line, or use \ to wrap the first line.

BowlesCR
  • 281
  • 1
  • 5
13

A note about the idea of using ifeq to do multi-line comments in make(1). They don't work very well since if you write the following:

ifeq (0,1)
    do not risk ifeq comments
    else trouble will find you
    ifeq is even worse
endif

The text between the ifeq and endif will still be parsed by make which means that you cannot write whatever you want in that section. And if you want to write a long comment and write whatever you want in the comment (including $ signs, colons and more which all have a meaning for make) then you must comment every single line. So why the ifeq...:)

Mark Veltzer
  • 1,394
  • 15
  • 20
  • That's not correct. The only "parsing" gmake does of the text inside the ifeq is to look for an endif. You can verify this easily with a makefile that has some bogus syntax inside the ifeq. As long as the condition in the ifeq evaluates as false, gmake will happily ignore the syntax errors inside the ifeq. – Eric Melski May 19 '15 at 20:51
  • 3
    That's not correct. Try the following example: ifeq (0, 1) do not risk ifeq comments else trouble will find you endif The **else** at the start of the line throws make into errors. So **else**, **ifeq**, **ifneq** and possibly many other symbols that I dont know about will give you problems. – Mark Veltzer May 19 '15 at 23:41
  • That's a fair point, but the majority of syntactic errors will be ignored. – Eric Melski May 20 '15 at 00:25
8
define BOGUS
lines
.....
endef
giuliolunati
  • 766
  • 5
  • 14
5

Not exactly what you're looking for, but similar in spirit. I don't expect it to be the accepted answer, but maybe it can help someone.

Assuming you're editing your makefiles in VIM:
Either decide which lines you want to comment or select them with 'v'.

Then you can use the regex s/^/#/ to comment out the lines
and s/^#// to revert them.

--Notes--

  • To open the vim command line, press : (colon)
  • To prep the command for the next 'n' lines, use .,+n
  • A sample line using "v" looks like: '<,'>s/^/#/
goodguy5
  • 422
  • 3
  • 16
  • 3
    Easier to use block mode instead. Go to the start of the first line to comment, ctrl-v, arrow down until the end, capital I, #, escape. To delete the comments: ctrl-v, arrow down all the way, x – brunch875 Feb 21 '20 at 17:24
3

In emacs, you can mark the region you want to comment out and hit M-; (which runs comment-dwim).

Dror
  • 12,174
  • 21
  • 90
  • 160