问题
Important edit: I've since solved the problem with my specific makefile, but I don't know how.
I replaced backslashes with forward slashes and it turns out my rules work fine with those. Gcc accepts mixed forward and back slashes, so it worked fine. A sticking point existed with mkdir
: it doesn't accept paths with mixed forward and back slashes. I changed my variables to include only back slashes in the Windows version, and now the file works perfectly.
For reference, here's some new rules and variables:
OUT = $(ROOT)\out
SRC = $(ROOT)\src
$(OUT):
mkdir $(OUT)
$(OUT)/%.o: $(SRC)/%.c
$(CC) $(CFLAGS) -o $@ -c $<
Why does minGW-make accept mixed slashes in rules after variable expansion but not backslashes-only? What exactly is going here?
Original post
I have a minGW install of make for windows. I copied my unix makefile and converted the commands to windows (and forward slashes to backslashes) but the resulting makefile does not work.
The main difference is that Windows has $(OUT) = $(ROOT)\out\win
and Unix has $(OUT) = $(ROOT)/out
. Unix works fine. make -f makefile.win
on windows complains that there is no rule for <root>\out\win\LinkedList.o
. The rules and error in question are:
make: *** No rule to make target `H:\CodeRIT\1014Syllables\out\win\LinkedList.o', needed by `H:\CodeRIT\1014Syllables\out\win\bin\syllables.exe'. Stop.
$(OUT)\%.o: $(ROOT)\src\%.c
gcc $(CFLAGS) -o $@ -c $<
$(OUT)\bin\syllables.exe: $(OUT)\LinkedList.o $(OUT)\Syllable.o $(OUT)\Model.o
gcc $(CFLAGS) -o $@ $^
$(OUT) should expand to <root>\out\win
giving the file it complains about a clear target. I don't understand why this rule doesn't work, given that the same rule works just fine on Unix. Is there some arcane rule about maximum directories in targets or something? I don't have enough experience with make to really assess what's going wrong here.
Here's the full text of the file in question: https://github.com/blastedt/syllables/blob/master/makefile.win
回答1:
$(OUT)\%.o:
does not define a pattern rule, you've escaped the %
.
$(OUT)\\%.o:
should work in this case, although it's because of gotchas like this that it's best to avoid backslashes when using make on Windows.
You will run into issues with shell commands like mkdir
and del
not working with forward slashes, you should consider switching to powershell, cygwin, MSYS2, or bash on windows.
来源:https://stackoverflow.com/questions/40455549/backslashes-in-windows-makefiles