[英]Makefile: Rule dependent on directory of target
I am trying to create a Makefile to create object files in a directory based on the source file.我正在尝试创建一个 Makefile 以在基于源文件的目录中创建 object 文件。 For example I have
DirA/file.c
and DirB/file.c
and I want to create a recipe for creating DirA/file.o
and DirB/file.o
.例如,我有
DirA/file.c
和DirB/file.c
并且我想创建一个用于创建DirA/file.o
和DirB/file.o
的配方。
rootdir/
|-- Makefile
|-- DirA
| |-- source.c
| +-- source.o
+-- DirB
|-- file.c
+-- file.o
I would assume the recipe would be similar to the following:我会假设配方类似于以下内容:
MAKEFLAGS += --no-builtin-rules
DirA_Files=$(wildcard DirA/*.c)
DirB_Files=$(wildcard DirB/*.c)
.PHONY: all a_files b_files
all: a_files b_files
@echo Done with all!
@echo built $(patsubst %.c,%.o,$(DirA_Files)) $(patsubst %.c,%.o,$(DirB_Files))
a_files: $(patsubst %.c,%.o,$(DirA_Files))
b_files: $(patsubst %.c,%.o,$(DirB_Files))
DirA/%.o DirB/%.o : DirA/%.c DirB/%.c
# Complex recipe that I'd rather not state twice in the Makefile
@echo "Building $@ because $? changed"
@touch $@
But I do not want the object files in DirA
to be dependent on the source files of DirB
, which is what the above recipe implies.但我不希望 DirA 中的
DirB
文件依赖于DirA
的源文件,这就是上面的配方所暗示的。
I have also tried我也试过
DirA/%.o : DirA/%.c
DirB/%.o : DirB/%.c
DirA/%.o DirB/%.o :
# Complex recipe that I'd rather not state twice in the Makefile
@echo "Building $@ because $? changed"
@touch $@
But then the $?
但然后
$?
variable is always blank.变量始终为空白。
How could I create a single recipe that would also allow me to build a_files
and b_files
independently我怎样才能创建一个也允许我独立构建
a_files
和b_files
的配方
You can't combine pattern rules quite that easily.您不能那么容易地组合模式规则。 But this situation is what defined recipes were made for:
但是这种情况是为以下定义的食谱而制作的:
define complex-rule
# Complex recipe that I'd rather not state twice in the Makefile
@echo "Building $@ because $? changed"
@touch $@
endef
DirA/%.o : DirA/%.c
$(complex-rule)
DirB/%.o : DirB/%.c
$(complex-rule)
related info:相关资料:
DirA_Files := $(wildcard DirA/*.c)
DirB_Files := $(wildcard DirB/*.c)
$(info DirA_Files: $(DirA_Files))
$(info DirB_Files: $(DirB_Files))
aobj := $(patsubst %.c,%.o,$(DirA_Files))
bobj := $(patsubst %.c,%.o,$(DirB_Files))
$(info aobj: $(aobj))
$(info bobj: $(bobj))
all: $(aobj) $(bobj)
@echo Done with all!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.