makefile如何将其转换为宏

时间:2014-09-29 17:14:09

标签: c++ makefile

在尝试编写非递归make时,我有以下块:

dir := $(ROOT)/libs/libA/
include $(dir)/rules.mk

dir := $(ROOT)/libs/libB/
include $(dir)/rules.mk

dir := $(ROOT)/libs/libC/
include $(dir)/rules.mk

这显然是不必要的重复。如何编写一个宏来折叠所有这些,以便我只需要提供libAlibBlibC?我还需要在每个dir之前设置include

3 个答案:

答案 0 :(得分:1)

LIBS := libA libB libC

include $(patsubst %, $(ROOT)/libs/%/rules.mk, $(LIBS))

答案 1 :(得分:1)

以下是如何在每个子目录列表中按顺序包含rules.mk生成文件,并使变量DIR等于每个rules.mk内的子目录。我根据需要使用变量名DIR而不是dir,因为我认为makefile中的“全局”变量名称应该是大写的。

有关非递归makefile的其他实现细节,请参阅

evbergen.home.xs4all.nl/nonrecursive-make.html

# include the makefile $2/rules.mk
# and make the variable $1 be equal to the directory $2, inside that makefile
define INCLUDE_DIR
$1 := $2
include $$($1)/rules.mk
endef

# do INCLUDE_DIR for directory variable name $1 and directories $2
define INCLUDE_DIRS
$(foreach dir, $2, $(eval $(call INCLUDE_DIR,$1, $(dir))))
endef


DIRS := \
    $(ROOT)/libs/libA \
    $(ROOT)/libs/libB \


$(call INCLUDE_DIRS,DIR, $(DIRS))

答案 2 :(得分:0)

如果您能够使用GNU make,则以下内容应该有效:

get_mk := $(ROOT)/libs/$(1)/rules.mk

include $(call get_mk,libA)
include $(call get_mk,libB)
include $(call get_mk,libC)

您可以http://www.gnu.org/software/make/manual/make.html#Call-Functioncall获取有关{{1}}的更多信息。