Can you make valid makefiles without tabs?

target: dependencies command1 command2 

On my system (Mac OS X), make seems to require the Makefile to have a tab character preceding the contents of each command line, or it will throw a syntax error.

It's annoying when creating or editing Makefiles, because I have my editor tuned to all spaces - all the time.

Can you make valid makefiles without tabs?

+78
whitespace makefile tabs spaces
Jan 25 '10 at 9:15
source share
8 answers

This is a syntactic fuzzy / make requirement, it has nothing to do with Mac OS X. Unfortunately, you can't do anything about it if you intend to use make .

Edit: GNU now supports custom recipe prefix. See this answer .

You are not the first to dislike this aspect of make . To quote the Unix Haters Handbook :

The problem with the Denniss Makefile is that when he added the comment line, he accidentally inserted a space before the tab character at the beginning of line 2. The tab character is a very important part of the syntax of Make files. All command lines (lines starting with cc in our example) should start with tabs. After he made his change, line 2 did not, therefore, an error.

"So what?" you ask: "What's wrong with that?"

There is nothing wrong. It’s just that when you look at how other programming tools work on Unix, using tabs as part of the syntax is similar to one of these pan traps in The Green Berets: a poor child from Kansas is the walking point in front of John Wayne and doesn't see the disconnect wire . After all, there are no wires for wires in Kansas corn fields. BOOM!

+83
Jan 25 '10 at 9:19
source share

There is a tricky way to have a valid tabless makefile.

If you changed your makefile, read:

 target: dependencies; command1; command2 

If it works. If you want it on more than one line, you can do:

 target: dependencies; \ command1; \ command2 

Restless, but it works.

+44
Dec 11 '10 at 22:39
source share

Since the time this question was asked, a version of GNU Make was released that allows you to use something other than Tab as the prefix symbol. From the mailing list ad :

New special variable: .RECIPEPREFIX allows you to reset the recipe introduction symbol from the default (TAB) to something else. The first character of this variable value is the introduction of a new character recipe. If the variable is set to an empty string, TAB is used again. It can be installed and reset as desired; recipes will use value when they were first analyzed. To detect this function, check the value of $ (. RECIPEPREFIX).

This feature was added to GNU Make 3.82, released in July 2010 (six months after this question the original request date). Since this, in turn, was three years old and has changed since then, it is likely that other Make flavors have followed GNU Make.

+32
Feb 20 '14 at
source share

If you have vimrc in your profile, you can add this line to prevent vim from changing in space:

 autocmd FileType make setlocal noexpandtab 

I also struggled with this, and this fixed it for me. Spread the good word!

+21
Oct. 14 '13 at 2:20
source share

In vim insert mode, you can use Ctrl-v <TAB> to insert a literal tab, even if you set the tab key to insert spaces. This, of course, does not answer your question, but may be an alternative to the methods available to avoid the need for literal tabs.

+7
Aug 15 '13 at 19:59 on
source share

It does it for me if you want to use spaces

 .RECIPEPREFIX += 

Example

+6
Oct 05 '14 at 7:08
source share

If you use EditorConfig , you can add the following lines to your .editorconfig file to force your IDE to use a tab for indentation instead of spaces in the Makefile :

 [Makefile] indent_style = tab 
+1
Feb 28 '17 at 13:45
source share

Not portable. Certain tastes are absolutely essential for tab characters. Another reason to prefer tabs over spaces :-)

-5
Jan 25 '10 at 9:19
source share



All Articles