我正在开展一个类似的项目:
rootdir/
Makefile
component1/
Makefile
component2/
Makefile
component3/
Makefile
lib1/
Makefile
lib2/
Makefile
lib3/
Makefile
Makefile.include
rootdir/component3/lib3/Makefile
的第一行是include Makefile.include
。
现在我想在一个名为test
的新目录中为lib3中的代码编写一些单元测试。所以我们改为使用这个结构:
rootdir/
Makefile
component1/
Makefile
component2/
Makefile
component3/
Makefile
lib1/
Makefile
lib2/
Makefile
lib3/
Makefile
Makefile.include
test/
Makefile
test1.c
test2.c
rootdir/component3/lib3/Makefile
内部有一条规则如下:
$(LIB3_LIB): $(LIB3_OBJ)
$(CC) $(LIB3_OBJ) -o $@
所以我觉得编写我的测试最自然的方法是在rootdir/component3/lib3/test/Makefile
中有一个看起来像这样的规则:
%.o : %.c $(LIB3_LIB)
$(CC) $(CFLAGS) -c $<
然后通过更多的步法,我将能够编写一个能够捕获所有测试的规则。
为了实现这一目标,我想我可以在include
中lib3/Makefile
lib3/test/Makefile
这样:
include ../Makefile
但是这个错误:
test$ make
../Makefile:1: Makefile.include: No such file or directory
make: *** No rule to make target `Makefile.include'. Stop.
这让我觉得它在Makefile.include
中寻找lib3/test
,这不是正确的事情。
我在make
docs的include
部分没有看到解决此问题的任何内容,我怀疑我是以错误的方式接近它。我错过了什么?
答案 0 :(得分:1)
你的观察是正确的。它尝试在rootdir/component3/lib3/test
子目录中包含Makefile.inc。
如果您只想确保重新编译库LIB3_LIB
,可以使用make -C ..
中Makefile中rootdir/component3/lib3/test
调用的子make。这将调用一个make,其中父目录作为当前工作目录,因此可以找到Makefile.inc
。
rootdir/component3/lib3/test/Makefile
看起来像:
LIB3_LIB=../lib3.lib
%.o : %.c $(LIB3_LIB)
$(CC) $(CFLAGS) -c $<
# call sub-make
$(LIB3_LIB): FORCE
$(MAKE) -C ..
# force sub-make
.PHONY: FORCE
FORCE:
必须使用虚假目标(此处名为FORCE
),以便始终调用sub-make。编译规则仍取决于库名称。因此,只有在子库中更改了库时,才会重新编译单元测试。
每个子make都会打印一条有关更改目录的消息。如果你想摆脱这个,把这一行添加到Makefile:
MAKEFLAGS += --silent #--no-print-directory