如何在makefile中创建一个目录

4

我正在使用Visual Studio 2005 nmake,我有一个像这样的测试makefile:

sometarget:
    -mkdir c:\testdir

我希望始终创建目录,而无需指定“sometarget”。例如,我可以这样做:

!if [if not exist c:\testdir\$(null) mkdir c:\testdir]
!endif

但这需要两行命令,而我只想执行“-mkdir c:\testdir”命令。如果我只用“-mkdir c:\testdir”替换它,nmake会报错:“fatal error U1034: syntax error : separator missing”。

我该如何始终执行mkdir命令,而不必处理!if []的内容呢?

5个回答

9
我想这会起作用:
 -@ if NOT EXIST "dir" mkdir "dir"

这对我在Windows 7上起作用,正是我所需要的。 - bdforbes

8

Make希望基于目标来完成任务,它不是一个通用的脚本工具。它查看目标并检查它们是否存在,如果目标不存在,则执行该目标下的命令。

通常的做法是设置一个虚拟目标,这个目标永远不会被make脚本生成,因此每次运行make时都必须执行相关命令。

或者,您可以将命令添加到批处理文件中,然后调用您的make文件。


3

我不确定在Windows中是否有类似nmake的东西,但是我成功地在Linux上创建了一个目录而不使用目标。我使用了"shell"函数来执行操作。例如:

# Find where we are
TOPDIR := $(shell pwd)
# Define destination directory
ROOTFS := $(TOPDIR)/rootfs
# Make sure destination directory exists before invoking any tags
$(shell [ -d "$(ROOTFS)" ] || mkdir -p $(ROOTFS))

all:
    @if [ -d "$(ROOTFS)" ]; then echo "Cool!"; else echo "Darn!"; fi

我希望Windows有类似的功能。

0

$(DIRNAME): @[ -d $@ ] || mkdir -p $@

$(DIRNAME): @[ -d $@ ] || mkdir -p $@


-2

尝试使用这个:

-mkdir -p c:\testdir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接