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

0 votes
asked Mar 17, 2010 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.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.

7 Answers

0 votes
answered Jan 17, 2010 by hlovdal

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

0 votes
answered Mar 17, 2010 by tom

Are you using Cygwin? Why not call rm?

0 votes
answered Mar 18, 2010 by tomlongridge

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
0 votes
answered Jan 20, 2015 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:

RM=cmd \/C del
0 votes
answered Jan 12, 2016 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:

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)
0 votes
answered Jan 18, 2016 by fraggamuffin

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.

0 votes
answered Sep 15, 2017 by jesuscf

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.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...