I want to use mingw to compile my C language project.
I found the command of make all
succeeded, however make clean
failed. There are only two files in my test project: test.c
and Makefile.mak
.
test.c
:
#include <stdio.h>
int main()
{
printf("Hello world\n");
while (1);
return 0;
}
Makefile.mak
:
all: test.o
gcc test.o -o test.exe
test.o: test.c
gcc -c test.c
clean:
@echo "clean project"
-rm test *.o
@echo "clean completed"
.PHONY: clean
When I ran make all -f Makefile.mak
, it succeeded and generated the expected test.exe
, and I also can run the executable. However, when I run make clean -f Makefile.mak
, it failed, the error is:
"clean project"
rm test *.o
process_begin: CreateProcess(NULL, rm test *.o, ...) failed.
make (e=2):
Makefile.mak:8: recipe for target 'clean' failed
make: [clean] Error 2 (ignored)
"clean completed"
Why?
EDIT:
The following link enlightened me: MinGW makefile with or without MSYS (del vs rm)
- I added the workaround code mentioned in the above link in my
makefile
:
ifeq ($(OS),Windows_NT)
RM = del /Q /F
CP = copy /Y
ifdef ComSpec
SHELL := $(ComSpec)
endif
ifdef COMSPEC
SHELL := $(COMSPEC)
endif
else
RM = rm -rf
CP = cp -f
endif
all: test.o
gcc test.o -o test.exe
test.o: test.c
gcc -c test.c
clean:
@echo "clean project"
-$(RM) test.exe *.o
@echo "clean completed"
.PHONY: clean
It works:
"clean project"
del /Q /F test.exe *.o
"clean completed"
- This reminds me that it may be because current environment doesn't support
rm
command, so I add mymsys
install path into environment path, then it works:
clean project
rm test.exe *.o
clean completed