4

我有以下代码来解压目录中的所有文件并将其移动到构建目录。如果我多次调用 make,即使构建目录已经存在,它也会每次尝试执行“构建”目标。有没有人遇到过这个?

我发现了这个问题,但它不一样。 Makefile 始终运行目标

操作系统:Ubuntu 12.04 程序:GNU Make 3.81

build: mkBuildDir untar 
    chmod 700 build

.PHONY: mkBuildDir untar 

mkBuildDir: 
    mkdir build 

untar: *.tar.gz
    for prefix in *.tar.gz; do \
        tar xvf $$prefix --directory=build; \
    done

clean: 
    rm -Rf build
4

1 回答 1

7

您链接的问题几乎相同。你永远不会创建一个名为 的文件mkBuildDir,所以它总是过时的,所以build总是过时的。

您的mkBuildDir目标没有做任何有用的事情(尽管我认为这是一个精简的 makefile)。如果你这样做了

# it'd be better to list the TARFILES explicitly, though this will probably work
TARFILES=`ls *.tar.gz`

all: build untar

build: $(TARFILES)
    test -d build || mkdir build
    chmod 700 build
    for prefix in $(TARFILES); do \
        tar xvf $$prefix --directory=build; \
    done

clean: 
    rm -Rf build

that would probably accomplish what you're looking for.

Having too many phony targets in a Makefile is usually a makefile 'code smell'. They are rarely the best/idiomatic way of doing things.

于 2012-08-24T11:31:18.547 回答