1

Let's say I have a makefile like the following.

foo: filename file\ name\ with\ spaces
    cat 'filename' 'file name with spaces' > foo

However, I don't have 2 files. I have a lot of them, so I am trying to make the makefile more consistent this way.

foo: filename file\ name\ with\ spaces
    cat $^ > foo

This works fine as long as the file names do not contain any spaces. Is it any way to put file names from $^ in single quotes? Or is there some other way to get around the problem of spaces?

EDIT

I found a partially solution.

foo: filename.txt file\ name\ with\ spaces.txt
    cat '$(subst .txt ,.txt' ',$^)' > foo

Unfortunately, this works only when all files have the same extension. I can not write script which works with any file name.

Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
  • 3
    Q: [Can GNU make handle filenames with spaces?](http://stackoverflow.com/questions/9838384/can-gnu-make-handle-filenames-with-spaces); A: Not really – Brent Bradburn Apr 25 '15 at 23:44
  • 1
    The _strong recommendation_ is to _not_ mix filenames with spaces in them and _make_. Just don't go there. Your life will become unreasonably miserable. Here you are just hitting one corner case: there is no way (not a clean one anyway) to distinguish the boundaries between these filenames, and the spaces contained in the filenames, in a _make_ list. – bobbogo Apr 27 '15 at 07:45
  • @bobbogo And what should I do if I have no influence on the file names? I get them from someone else, and I have to just write the makefile. – Piotr Siupa Apr 27 '15 at 09:58
  • Hmmm. At a pinch you could make a tree of symbolic links shadowing your original file set, replacing spaces with dash say (`file name with spaces` is linked to `file-name-with-spaces` etc.). [This will work on windows with cygwin _make_ and cygwin symbolic links if you are on that platform.] – bobbogo Apr 27 '15 at 17:16
  • @bobbogo That sounds much worse than putting the file names in the makefile twice. – Piotr Siupa Apr 27 '15 at 17:21
  • It doesn't matter how many times you put the filenames-with-spaces in a makefile, it's not going to help you (you've already discovered `$^` becomes unusable in this scenario) – bobbogo Apr 28 '15 at 15:51

0 Answers0