powershell - 为什么我在 makefile 中的“干净”规则不起作用?
问题描述
我是新手,正在玩makefile。我确实掌握了有关 make 和 makefile 如何工作以及如何编写非常基本的规则的介绍性知识。在互联网上的各种文章中,有使用干净规则删除对象或可执行文件的示例。这很简单,
clean :
rm -f filename
我发现 ,rm -f filename
在 powershell 中不起作用(至少对我而言),因此使用rm filename
which 在 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
好奇这是什么?
解决方案
我经常使用带有 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 跨平台、更明确且更易于开发,因为我可以使用我/我的团队熟悉的脚本语言。
推荐阅读
- azure - App Insights 中的 client_Browser 字段始终为空
- c# - 浏览器因对设备有害而阻止下载
- c# - 是否可以使用 Url.Action 仅返回控制器名称和操作?
- python - 使用 Python 将 ADODB 记录集写入数据透视缓存
- javascript - Lerna 避免符号链接
- ssl - Tomcat - SSL 证书配置
- rest - Acumatica Web 服务 - 从通用查询中获取数据
- php - laravel reactjs 将所有 api 请求放在一个 get 请求中
- rhel - CentOS 8 中的屏幕共享
- ios - 快速将viewcontroller拖到tabbarcontroller时黑屏可见?