首页 > 解决方案 > 为什么我在 makefile 中的“干净”规则不起作用?

问题描述

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

clean :
    rm -f filename

我发现 ,rm -f filename在 powershell 中不起作用(至少对我而言),因此使用rm filenamewhich 在 powershell 中起作用。

我的生成文件:

CC=gcc
CFLAGS=-Wall -g

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

%.c:
    subl $@

clean:
    rm hello.exe

我在工作目录中有几个文件,我希望我的干净规则删除“hello.exe”文件。以前的规则确实有效,但mingw32-make给出了错误。我不知道为什么,在 powershell 中明确运行相同的命令(配方)后,它可以工作。也没有任何名为 clean 的文件来创建冲突。我所知道的是该规则被调用,但是rm hello.exe从makefile调用时不起作用。

电源外壳

从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\1.0\bin\make.exe好奇这是什么?

标签: powershellmakefile

解决方案


我经常使用带有 powershell 核心命令的 GNU Makefiles。我在我的 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 跨平台、更明确且更易于开发,因为我可以使用我/我的团队熟悉的脚本语言。


推荐阅读