How to avoid whitespace inside a Makefile
Let's say I want to compile a C ++ project located in /home/me/Google Drive/Foobar/
to an executable file named Foobar
and I want to use GNU Make to make this process automatic.
This is what my Makefile looks like:
OUTPUT = $(notdir $(CURDIR))
all $(OUTPUT):
g++ *.cpp -o $(OUTPUT)
The problem is that there are spaces in the project path, the command notdir
interprets two separate paths and returns Google Foobar
.
I've tried putting quotes around $(CURDIR)
( $(notdir "$(CURDIR)")
), but I'm getting the following error:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'
/bin/sh: -c: line 1: syntax error: unexpected end of file
Makefile:4: recipe for target 'all' failed
make: *** [all] Error 1
I don't understand where this problem comes from.
Also, I would appreciate an answer that does not involve changing the path name ... Thanks :)
source to share
The problem is that the problem with GNU make in directory storage is less than on the extension side of the shell. So change
g++ *.cpp -o $(OUTPUT)
in
g++ *.cpp -o "$(OUTPUT)"
Using a GNU make variant called remake you can see this more easily.
With the remake, if you want to check if $ OUTPUT has a space in it, you can run:
$ remake -X
GNU Make 4.1+dbg0.91
...
-> (/home/me/Google Drive/Makefile:3)
all:
remake<0> expand OUTPUT
Makefile:1 (origin: makefile) OUTPUT := Google Drive
Now, if you want to see a shell script that does the same things as GNU make, use the write command:
remake<1> write all
File "/tmp/all.sh" written.
everything above is the name of the make target. Then take a look /tmp/all.sh
at which is a shell script and you will see that you have a problem, which of course can be fixed by adding quotes.
source to share