从 GNU makefile 调用 Windows 命令(例如 del)

发布于 2024-08-25 03:01:45 字数 417 浏览 4 评论 0原文

使用GNU Make 似乎无法调用Windows 系统命令(例如del、move 等)。我正在尝试创建一个不依赖于用户安装额外工具(例如来自 Cygwin 的 rm.exe)的 makefile。

运行下面的规则时,报错del: command not find

clean:
   del *.o

这大概是因为没有“del”这样的可执行文件。我也尝试过将其作为 cmd 的选项运行,但这似乎只会打开一个新提示:

clean:
    cmd /C del *.o

我正在使用 Windows XP (5.1.2600) 和捆绑的 GNU Make 3.79.1作为 MSys 的一部分。

It does not appear to be possible to call Windows system commands (e.g. del, move, etc) using GNU Make. I'm trying to create a makefile that doesn't rely on the user having extra tools (e.g. rm.exe from Cygwin) installed.

When the following rule is run, an error is reported del: command not found:

clean:
   del *.o

This is presumably because there is no such execuatable as "del". I've also tried running it as an option to cmd but with this only seems to open a new prompt:

clean:
    cmd /C del *.o

I'm using Windows XP (5.1.2600) with GNU Make 3.79.1 that is bundled as part of MSys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

情话墙 2024-09-01 03:01:45

似乎 /C 开关需要转义,因为 / 在 GNU Make 中被解释为路径。以下内容按预期工作:

clean:
    cmd //C del *.o

It seems the /C switch needs to be escaped because a / is interpreted as a path in GNU Make. The following works as expected:

clean:
    cmd //C del *.o
对不⑦ 2024-09-01 03:01:45

因为基于 DOS 的系统有两个不同的命令用于删除文件和目录,所以我发现有两个不同的定义效果最好:

ifeq ($(OS),Windows_NT)
    RM = cmd //C del //Q //F
    RRM = cmd //C rmdir //Q //S
else
    RM = rm -f
    RRM = rm -f -r
endif

clean:
    $(RM) $(TARGET).elf $(TARGET).map
    $(RRM) $(BUILD_DIR)

Because DOS-based systems have two different commands for removing files and directories, I find that having two different defines works the best:

ifeq ($(OS),Windows_NT)
    RM = cmd //C del //Q //F
    RRM = cmd //C rmdir //Q //S
else
    RM = rm -f
    RRM = rm -f -r
endif

clean:
    $(RM) $(TARGET).elf $(TARGET).map
    $(RRM) $(BUILD_DIR)
热血少△年 2024-09-01 03:01:45

我也发生过。在 makefile 的顶部添加:

SHELL=cmd

由于您是在 Windows 上编译,因此选择“cmd”作为默认 shell。这
这很重要,因为 GNU make 将搜索类似 linux/unix shell 的路径,如果找到,它将使用它。安装cygwin时就是这样。此行为的副作用是找不到“del”和“echo”等命令。如果我们告诉 GNU make 使用“cmd”作为其 shell,那么“del”等将可用。

Happened to me too. At the top of your makefile add:

SHELL=cmd

Since you are compiling on windows, select 'cmd' as the default shell. This
is important because GNU make will search the path for a linux/unix like shell and if it finds one it will use it instead. This is the case when cygwin is installed. The side effect of this behavior is that commands like 'del' and 'echo' are not found. If we tell GNU make to use 'cmd' as its shell, then 'del' and such will be available.

彼岸花似海 2024-09-01 03:01:45

delcmd.exe(以及之前的 command.com)的内置命令。你的命令 cmd /C del *.o 应该可以工作,如果它启动一个新的命令提示符,我怀疑 cmd 可能是一个包装器。您是否尝试过使用其完整路径(例如c:/WINDOWS/system32/cmd.exe)来调用cmd?

del is a builtin command of cmd.exe (as well as previously command.com). Your command cmd /C del *.o should work, if it starts a new command prompt I suspect that cmd maybe might be a wrapper. Have you tried to call cmd with its full path (e.g. c:/WINDOWS/system32/cmd.exe)?

甜心小果奶 2024-09-01 03:01:45

Tom Longridge 的答案对我来说接近事实,但转义需要在 Windows Vista Business 计算机上的正斜杠之前使用反斜杠来完成,我需要这样做:

RM=cmd \/C del

Tom Longridge's answer was close to the truth for me, but the escaping needed to be done using a backslash before the forward slash on the Windows Vista Business machine I was needing this for:

RM=cmd \/C del
梅倚清风 2024-09-01 03:01:45

另一个解决方案是创建一个 del.bat 文件,其中包含:

@echo off
del %*

然后 makefile 可以简单地包含

clean:
   del *.o

此内容以清理 makefile,但可能会使您的构建目录稍微混乱。

Another solution is to create a del.bat file containing:

@echo off
del %*

then the makefile can simply contain

clean:
   del *.o

this cleans up the makefile, but may clutter your build directory slightly.

霓裳挽歌倾城醉 2024-09-01 03:01:45

我刚刚完成这个。我不确定 DOS/Windows 中的情况是否发生了变化,但这就是当前版本必须格式化的方式。

OBJ=o
BIN=bin


clean:
    cmd /C del $(OBJ)\\*.o 
    cmd /C del $(BIN)\\*.exe

I just completed this. I am not sure if things have changed in DOS/Windows but this was how it had to be formatted with current versions.

OBJ=o
BIN=bin


clean:
    cmd /C del $(OBJ)\\*.o 
    cmd /C del $(BIN)\\*.exe
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文