Can you make valid Makefiles without tab characters?

前端 未结 10 2582
孤城傲影
孤城傲影 2020-12-02 07:17
target: dependencies
    command1
    command2

On my system (Mac OS X), make seems to require that that Makefiles have a tab character

相关标签:
10条回答
  • 2020-12-02 07:30

    Until GNU Make 4.2

    Steven Penny's answer works.

    .RECIPEPREFIX +=
    

    The reason why this works is described in my comment.


    Since GNU Make 4.3 (released on 19 Jan 2020)

    Behavior of += operator has been changed in a backward-incompatible way. If the left operand has an empty value, a space is no longer added.

    You can instead use

    .RECIPEPREFIX := $(.RECIPEPREFIX)<space>
    

    , where <space> is a single space. Although $(.RECIPEPREFIX) is expanded as an empty value, this is needed not to let GNU Make ignore <space>. Note this code works even on GNU Make older than version 4.3.

    0 讨论(0)
  • 2020-12-02 07:33

    Not portably. Certain flavours of make absolutely require tab characters. Yet another reason for preferring tabs over spaces :-)

    0 讨论(0)
  • 2020-12-02 07:38

    in ubuntu: vi Makefiles replace space by tab (or anything else you want):

    :%s/<space chars>/^I/g
    

    For ex replace 8 spaces by tab:

    :%s/        /^I/g
    

    Be attention: ^I insert with tab key, not ^ and I characters :D

    0 讨论(0)
  • 2020-12-02 07:41

    In vim's insert mode, one can use Ctrl-v <TAB> to insert a literal tab, even if you have set the tab key to insert spaces. This doesn't answer your question, of course, but might be an alternative to the methods available to avoid needing literal tabs.

    0 讨论(0)
  • 2020-12-02 07:42

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

    [Makefile]
    indent_style = tab
    
    0 讨论(0)
  • 2020-12-02 07:46

    This is a syntax oddity/requirement of make, it has nothing to do with Mac OS X. Unfortunately, there's nothing you can do about it if you are going to use make.

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

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

    The problem with Dennis’s Makefile is that when he added the comment line, he inadvertently 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 Makefiles. All command lines (the lines beginning with cc in our example) must start with tabs. After he made his change, line 2 didn’t, hence the error.

    “So what?” you ask, “What’s wrong with that?”

    There is nothing wrong with it, by itself. It’s just that when you consider how other programming tools work in Unix, using tabs as part of the syntax is like one of those pungee stick traps in The Green Berets: the poor kid from Kansas is walking point in front of John Wayne and doesn’t see the trip wire. After all, there are no trip wires to watch out for in Kansas corn fields. WHAM!

    0 讨论(0)
提交回复
热议问题