为什么我在 makefile 中的 "clean" 规则不起作用?

Why is my "clean" rule in makefile not working?

我是新手,正在研究 makefile。我确实掌握了有关 make 和 makefile 如何工作以及如何编写非常基本的规则的入门知识。在 Internet 上的各种文章中,有使用干净规则删除对象或可执行文件的示例。非常简单,

clean :
    rm -f filename

我发现 rm -f filename 在 powershell 中不起作用(至少对我而言),所以我使用了在 powershell 中起作用的 rm filename

我的生成文件:

CC=gcc
CFLAGS=-Wall -g

%: %.c
    $(CC) -o $@ $< $(CFLAGS)
    ./$@

%.c:
    subl $@

clean:
    rm hello.exe

我在工作目录中有几个文件,我希望我的清理规则删除 'hello.exe' 文件。以前的规则确实有效,但是 mingw32-make 给出了一个错误。我不知道为什么,在 运行 相同的命令(配方)在 powershell 中显式执行后,它起作用了。也没有任何名为 clean 的文件会产生冲突。我所知道的是正在调用规则,但是从 makefile 调用时 rm hello.exe 不起作用。

来自 powershell,(以防图像加载不正确)

PS F:\home\learnC> ls


    Directory: F:\home\learnC


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        04-09-2021     21:31                articles
d-----        31-07-2021     10:06                books
-a----        02-08-2021     21:45           1872 .hello.c.un~
-a----        12-08-2021     12:23          40766 a.exe
-a----        04-09-2021     21:58            410 format.c
-a----        04-09-2021     21:58          42446 format.exe
-a----        04-09-2021     21:10            211 hello.c
-a----        05-09-2021     13:04          41934 hello.exe
-a----        05-09-2021     12:57            110 makefile


PS F:\home\learnC> rm -f hello.exe
Remove-Item : Parameter cannot be processed because the parameter name 'f' is ambiguous. Possible matches include:
-Filter -Force.
At line:1 char:4
+ rm -f hello.exe
+    ~~
    + CategoryInfo          : InvalidArgument: (:) [Remove-Item], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameter,Microsoft.PowerShell.Commands.RemoveItemCommand

PS F:\home\learnC> mingw32-make clean
rm hello.exe
process_begin: CreateProcess(NULL, rm hello.exe, ...) failed.
make (e=2): The system cannot find the file specified.
makefile:12: recipe for target 'clean' failed
mingw32-make: *** [clean] Error 2
PS F:\home\learnC> rm hello.exe
PS F:\home\learnC> ls


    Directory: F:\home\learnC


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        04-09-2021     21:31                articles
d-----        31-07-2021     10:06                books
-a----        02-08-2021     21:45           1872 .hello.c.un~
-a----        12-08-2021     12:23          40766 a.exe
-a----        04-09-2021     21:58            410 format.c
-a----        04-09-2021     21:58          42446 format.exe
-a----        04-09-2021     21:10            211 hello.c
-a----        05-09-2021     12:57            110 makefile


PS F:\home\learnC>
  

我也尝试按照评论部分的建议对 rm 使用详细命令,但它仍然会出错。

PS F:\home\learnC> mingw32-make clean
Remove-Item -Force hello.exe
process_begin: CreateProcess(NULL, Remove-Item -Force hello.exe, ...) failed.
make (e=2): The system cannot find the file specified.
makefile:12: recipe for target 'clean' failed
mingw32-make: *** [clean] Error 2
PS F:\home\learnC>

如何修复此错误?还有什么方法可以使用 make 而不是 mingw32-make

C:\MinGW\bin\mingw32-make.exe我用的那个

C:\MinGW\msys.0\bin\make.exe好奇这是什么?

我经常将 GNU Makefile 与 powershell 核心命令一起使用。我在我的 makefile 顶部使用此片段更改为 pwsh:

ifeq ($(OS),Windows_NT)
    SHELL := pwsh.exe
else
   SHELL := pwsh
endif
.SHELLFLAGS := -NoProfile -Command 

我刚刚做了一个快速测试,如果没有这个片段,我会得到和你一样的错误。当我将它添加到我的 makefile 时,rm hello.exe 命令按预期工作。

我不知道mingw32和gnu make的区别。 GNU make 对我来说很可靠,使用 pwsh 核心有助于使我的 makefile 跨平台,更明确,并且更容易开发,因为我可以使用 me/my 团队熟悉的脚本语言。