Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6251751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:40:19+00:00 2026-05-24T13:40:19+00:00

Because GNU make allows variables to be as large as memory allows, it has

  • 0

Because GNU make allows variables to be as large as memory allows, it has no problem building massive dependency lists. However, if you want to actually use these lists of files in a recipe (sequence of shell commands for building a target), you run into a problem: the command might exceed the shell’s command line length limit, producing an error such as “Argument list too long”.

For example, suppose I want to concatenate several files contained in the list $(INPUTS) to produce a file combined.txt. Ordinarily, I could use:

combined.txt: $(INPUTS)
        cat $^ > $@

But if $(INPUTS) contains many thousands of files, as it does in my case, the call to cat is too long and fails. Is there a way to get around this problem in general? It’s safe to assume that there exists some sequence of commands that have identical behaviour to the one enormous command — in this case, a series of cat commands, one per input file, that use >> to append to combined.txt would work. But how can make be persuaded to generate those commands?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:40:20+00:00Added an answer on May 24, 2026 at 1:40 pm

    In looking for the answer, about the best suggestion I could find was to break up the list into a series of smaller lists and process them using shell for loops. But you can’t always do that, and even when you can it’s a messy hack: for example, it’s not obvious how to get the usual make behaviour of stopping as soon as a command fails. Luckily, after much searching and experimentation, it turns out that a general solution does exist.

    Subshells and newlines

    make recipes invoke a separate subshell for each line in the recipe. This behaviour can be annoying and counterintuitive: for example, a cd command on one line will not affect subsequent commands because they are run in separate subshells. Nevertheless it’s actually what we need to get make to perform actions on very long lists of files.

    Ordinarily, if you build a “multiline” list of files with a regular variable assignment that uses backslashes to break the statement over multiple lines, make removes all newlines:

    # The following two statements are equivalent
    FILES := a b c
    
    FILES := \
    a \
    b \
    c
    

    However, using the define directive, it’s possible to build variable values that contain newlines. What’s more, if you substitute such a variable into a recipe, each line will indeed be run using a separate subshell, so that for example running make test from /home/jbloggs with the makefile below (and assuming no file called test exists) will produce the output /home/jbloggs, because the effect of the cd .. command is lost when its subshell ends:

    define CMDS
    cd ..
    pwd
    endef
    
    test:
            $(CMDS)
    

    If we create a variable that contains newlines using define, it can be concatenated with other text as usual, and processed using all the usual make functions. This, combined with the $(foreach) function, allows us to get what we want:

    # Just a single newline!  Note 2 blank lines are needed.
    define NL
    
    
    endef
    
    combined.txt: $(INPUTS)
            rm $@
            $(foreach f,$(INPUTS),cat $(f) >> $@$(NL))
    

    We ask $(foreach) to convert each filename into a newline-terminated command, which will be executed in its own subshell. For more complicated needs, you could instead write out the list of filenames to a file with a series of echo commands and then use xargs.

    Notes

    • The define directive is described as optionally taking a =, := or += token on the end of the first line to determine which variable flavour is to be created — but note that that only works on versions of GNU make 3.82 and up! You may well be running the popular version 3.81, as I was, which silently assigns nothing to the variable if you add one of these tokens, leading to much frustration. See here for more.
    • All recipe lines must begin with a literal tab character, not the 8 spaces I have used here.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In building a C++ project with the GNU tool chain, make tells me ~
I am using gnu Make 3.82 and have an annoying problem. I have a
How do I force GNU Make variables to evaluate immediately? Consider the following example:
Because I run a blog aggregator website which checks a large list of RSS
all. I've been banging my head for a few hours because of this problem.
Setup: I installed Cygwin with the GNU programming lineup (gcc, g++, make, and gdb)
Using GNU make, is it possible to create a set of targets that will
We use GNU Make for our system. At the end of our makefiles, we
I have a collection of examples that I want to make sure they fail
I am using non-gnu C compiler which doesn't support dependency generation option (-MM in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.