开发者

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

开发者 https://www.devze.com 2022-12-23 19:06 出处:网络
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

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.


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


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)


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.


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)?


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


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号