从 GNU 生成文件调用 Windows 命令(例如 del)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2463037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:08:21  来源:igfitidea点击:

Calling Windows commands (e.g. del) from a GNU makefile

windowsmakefile

提问by TomLongridge

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.exefrom Cygwin) installed.

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

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

运行以下规则时,报错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 cmdbut with this only seems to open a new prompt:

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

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.

我使用 Windows XP (5.1.2600) 和 GNU Make 3.79.1,它是 MSys 的一部分。

回答by TomLongridge

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

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

clean:
    cmd //C del *.o

回答by Tom

Are you using Cygwin? Why not call rm?

你在使用 Cygwin 吗?为什么不打电话rm

回答by hlovdal

delis a builtin command of cmd.exe(as well as previously command.com). Your command cmd /C del *.oshould 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)?

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

回答by J P

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

因为基于 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)

回答by jesuscf

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

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

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.

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

回答by xarragon

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:

Tom Longridge 的回答对我来说接近事实,但需要在 Windows Vista Business 机器上的正斜杠之前使用反斜杠完成转义,我需要它:

RM=cmd \/C del

回答by FraggaMuffin

Another solution is to create a del.batfile containing:

另一种解决方案是创建一个del.bat包含以下内容的文件:

@echo off
del %*

then the makefilecan simply contain

那么makefile可以简单地包含

clean:
   del *.o

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

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