【问题标题】:make clean failed in windows在 Windows 中进行清理失败
【发布时间】:2018-08-29 06:32:23
【问题描述】:

我想用mingw来编译我的c语言项目。 我发现“make all”的命令成功了,但是“make clean”失败了。 我的测试项目里只有两个文件,一个是test.c,一个是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

当我运行 make all -f Makefile.mak 时,它成功并生成了预期的 test.exe,我也可以运行它。但是,当我运行 make clean -f Makefile.mak 时,它失败了,错误信息是:

"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"

为什么...

编辑:

以下链接启发了我, MinGW makefile with or without MSYS (del vs rm)

1) 我在我的 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

有效:

"clean project"
del /Q /F test.exe *.o
"clean completed"

2)这提醒我可能是因为当前环境不支持rm命令,所以我将我的msys安装路径添加到环境路径中,然后它可以工作:

clean project
rm test.exe *.o
clean completed

【问题讨论】:

  • 我不认为 mingw 有 rm 命令,但我可能是错的。 cygwin 是 POSIX,并且有 rm
  • 我建议,如果您找到自己问题的答案,而不是在包含答案的情况下扩展问题,而是单独回答您自己的问题。

标签: c makefile mingw


【解决方案1】:

您正在尝试删除名为“test”的文件,但不存在这样的文件。结果,rm 命令失败。

您想删除“test.exe”,因为它是您的输出文件的名称。此外,您应该使用-f 选项到rm,因为这将 1) 执行强制删除,并且 2) 如果文件不存在,则不会失败:

clean:
    @echo "clean project"
    -rm -f test.exe *.o
    @echo "clean completed"

【讨论】:

  • 感谢您的回答,我根据您的建议更改了makefile,但仍然失败...
【解决方案2】:

试试这个:

'make clean' not working

默认情况下,MinGW 中的 Clean 将运行 -rm 命令。但是window不支持这个命令。窗口使用del。

所以你需要用notepad++编辑makefile,改变

clean:
    -rm -fR $(BUILD_DIR)

clean:
    -del -fR $(BUILD_DIR)

【讨论】:

    猜你喜欢
    • 2019-12-24
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    相关资源
    最近更新 更多