3

我正在尝试创建一个可以在调用时递归构建/清理的 Makefile。我的构建工作正常,但是 clean 命令出现错误。

我的目录结构是这样的:

main
 |
 +-- Makefile
 |
 +-- common
 |      |
 |      +-- gcas_debug
 |      |      |
 |      |      +-- Makefile
 |      + -- gcas_nvdata
 |             |
 |             +-- Makefile
 +-- gcinit
        +-- Makefile

当我make在主目录中调用时,它会按照我的意愿进行构建并构建所有内容,但是当我调用make clean它时:

mike@mike-VirtualBox:~/iCOM/framework$ make clean
for d in common/gcas_debug common/gcas_nvdata; \
    do \
    make --directory=$f clean; \
    done
make: the `-C' option requires a non-empty string argument
Usage: make [options] [target] ...
Options:
  -b, -m                      Ignored for compatibility.
...

我不明白为什么 clean 命令不起作用...有什么建议吗?
完整(主要)Makefile:

lib_gcas_debug := common/gcas_debug
lib_gcas_nvdata := common/gcas_nvdata
libraries := $(lib_gcas_debug) $(lib_gcas_nvdata)
DESTDIR=$(PWD)/output
bindir=

gc_init := gcinit
EXE := $(gc_init)/$(gc_init)

.PHONY: all $(gc_init) $(libraries)
all: $(gc_init)

$(gc_init) $(libraries):
    $(MAKE) --directory=$@

$(gc_init): $(libraries)

install: $(gc_init)
    install -d -m 0755 $(DESTDIR)$(bindir)
    install -m 0755 $(EXE) $(DESTDIR)$(bindir)

clean:
    for d in $(libraries); \
    do \
    $(MAKE) --directory=$$f clean; \
    done

编辑:如果我交换我for d得到for $d

mike@mike-VirtualBox:~/iCOM/framework$ make clean
for  in common/gcas_debug common/gcas_nvdata; \
    do \
    make --directory= clean; \
    done
/bin/sh: -c: line 0: syntax error near unexpected token `common/gcas_debug'
/bin/sh: -c: line 0: `for  in common/gcas_debug common/gcas_nvdata; \'
make: *** [clean] Error 1
4

2 回答 2

4

这对我有用:

SUBDIRS := foo bar baz

.PHONY: subdirs $(SUBDIRS) clean all

subdirs: $(SUBDIRS) 

$(SUBDIRS):
    $(MAKE) -C $@ $(MAKECMDGOALS)

clean: $(SUBDIRS)
    rm -rf dist

dist: $(SUBDIRS) dist/.build_marker

dist/.build_marker: 
    mkdir -p dist
    for d in $(SUBDIRS) ; do cp $$d/dist/* dist ; done
    touch dist/.build_marker

我只需要使用dist目标中的 for 循环来复制文件。这允许make -j构建并行性,更不用说更易于阅读的 Makefile。

于 2018-09-25T17:34:46.413 回答
3

Read the for loop again:

for d in [...]; do make --directory=$f

Didn't you mean $d, as in

for d in [...]; do make --directory=$d

So, the Makefile should look:

for d in common/gcas_debug common/gcas_nvdata; \
do \
    make --directory=$d clean; \
done
于 2013-03-11T20:06:04.663 回答