help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: What is the difference between > and >> in a makefile.


From: Paul Smith
Subject: Re: What is the difference between > and >> in a makefile.
Date: Mon, 28 Mar 2011 15:37:08 -0400

On Mon, 2011-03-28 at 11:15 -0700, givemecode wrote:
> I modified a for loop in a makefile that looked like this:
> 
>       @for f in $(DRV_GEN_HEADER) ; do echo "#include \""$$f"\"" >> $@ ; done
> 
> which puts the output of echo into a new file I sepcified.
> 
> changed to this:
> 
>        @for f in $(INCLUDE_DIR)/%.h); do \
>          echo "#include \""$$f"\""; \
>         done > '$@'
> 
> which does the same thing but the writing is done after the loop completes
> (I think). Can someone please explain the difference and why it works?

This is a question about shell programming, not about makefiles and is
probably better asked in help list for shell hacking.

However, I can answer it for you :-)

Note that the latter does NOT do all the writing after the loop
completes.  The file is written to, one line at a time, just as in the
first example.

The first example says for each file, echo this one line and append it
(hence ">>" which is append) to the file address@hidden

The second example says, for each file echo this one line, and redirect
all output from all commands run inside the for-loop to overwrite the
file $@ (hence ">" which is overwrite--i.e., truncate and write from
scratch).

Some differences between the two:

     1. If the file $@ exists before the command is invoked, then the
        first version will append the new content to the
        already-existing content... this is probably not what you want
        in this situation.  The second version will always recreate the
        file from scratch.
     2. If you wanted to print some content to stdout and some to the
        file, then you have to use the first version because the second
        version will put all stdout from all commands in the for loop
        into the file (well, you can play fancy tricks with file
        descriptors but it's hardly worth it).





reply via email to

[Prev in Thread] Current Thread [Next in Thread]