Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most reliable way of using GNUMake with filenames containing spaces?

I want to use GNUMake to run a rule-based makefile which builds a set of C files in a directory structure (on a Windows file system).

The root directory, some sub-directories and some files contain spaces.

Example file: "C:\Documents and Settings\<username>\My Documents\Test Dir\Build Me.c"

GNUMake doesn't really work when the file paths contain spaces. I've read about the possible ways of working around this issue (removing the spaces from my filenames, using the 8.3 format, substituting spaces with ? or \\ etc.) but none of them are perfect (or are they?)

Is there a silver bullet that will solve this problem?

BTW I am stuck with GNUMake, I can't use a different make tool.

like image 687
demoncodemonkey Avatar asked Mar 20 '09 23:03

demoncodemonkey


2 Answers

The easiest thing is indeed to fix the file names.

Failing that, though, write your commands to put double quotes around the file names. The easiest and safest thing is to put all the file names into macros; the trick there is that you have to escape the double quotes, which Make is otherwise going to want to eat up itself.

So: FN="\"C:\My Documents\myfiles.c\"" FN2="C:\My Documents\myfile2.c"

or use $(CC) $(CFLAGS) "$(FN2)"

The trick here is to echo your command line with echo

echo $(CC) $(CFLAGS) "$(FN2)"

or use make -d to get all the details of what make is trying to do.

You may need to hack about with this a bit, in particular, you may need to double up the escapes

like image 194
Charlie Martin Avatar answered Oct 21 '22 15:10

Charlie Martin


You may be able to escape the spaces in your makefile, i.e.:

$(CC) $(CFLAGS) 'C:\Documents\ and\ Settings\<username>\My\ Documents\Test\ Dir\Build Me.c'

I've added the single quotes just in case, but I don't know if this works if you're using the windows terminal (rather than cygwin etc).

like image 27
Dana the Sane Avatar answered Oct 21 '22 13:10

Dana the Sane