【问题标题】:GNU make how to make targets in different directoryGNU make 如何在不同的目录中创建目标
【发布时间】:2022-01-02 15:14:38
【问题描述】:

我想在与 Makefile 所在目录不同的目录中生成目标。它涉及使用 *.in 文件的 M4 宏处理步骤的输出。 我无法正确理解模式规则;我推测目标定义不正确,但是如何指定呢?项目结构如下

                prj
                 +
                 |
        -+-------+-----------+---------------+
         |                   |               |
        inc                 src            config
         |                   |               |
        a1-1.h           +----------+--     conf.m4 
        a1-2.h           |
                        app
                         |
                         a1-1.in
                         a1-2.in
                         Makefile

Makefile 代码:

M4INSRCS=$(wildcard *.in)
HTARGETS=$(M4INSRCS:.in=.h)
HDIR=../../inc

.PHONY : all
all: $(HTARGETS) $(MINSRCS)
    @echo "INSRCS $(M4INSRCS)"
    @echo "HTARGETS $(HTARGETS)"
    @echo "HDIR $(HDIR)"

$(HDIR)/%.h : %.in
    cp $< $(HDIR)/$@
    touch $(HDIR)/$@

制作报告: make: *** 没有规则来制作目标a1-1.h', needed by all'。停下来。

【问题讨论】:

  • 好吧,all 依赖于a1-1.h,但您尚未定义构建该目标的规则。所以 make 告诉你构建它没有规则。
  • 其实目标是 ../../inc/a1-1.h;我想我尝试使用 Makefile 中的文本处理来为每个目标构建一个 mk 包含
  • 您已将 HTARGETS 设置为值 a1-1.h a1-2.h(修正示例中的错字)。然后你列出all: $(HTARGETS)。所以all 目标取决于a1-1.ha1-2.h 的先决条件,而这些是make 将尝试构建的文件。你还没有告诉 make 如何构建这些目标,所以 make 给出了一个错误。如果您想构建不同于 a1-1.ha1-2.h 的东西,那么您必须更改您要求它构建的先决条件。

标签: makefile directory target


【解决方案1】:

我认为“所有”目标先决条件 HTARGETS 和 $(HDIR)/%.h 规则之间存在链接。显然不是。所以我用 HDIR 前缀完成了 HTARGET 元素(文件名)。现在我得到了想要的结果

M4INSRCS=$(wildcard *.in)   # the set of input file names
HFILES=$(M4INSRCS:.in=.h)   # the set of target file names
HDIR=../../inc              # the place to store the target files
CONF_DIR=../../config
CONF_FNAM=conf.m4
CONFIG=$(CONF_DIR)/$(CONF_FNAM)

# get HTARGETS to be of the format dir/dir/target-file-name
HTARGETS=$(foreach FILE,$(HFILES),$(HDIR)/$(FILE))
#HTARGETS now have format ../../inc/a1-1.h ../../inc/a1-2.h . . . .

# output depends on actual state of targets, *.in files and a config file
all: $(HTARGETS) $(M4INSRCS) $(CONFIG)

# here we have the pattern for a HTARGETS element
$(HDIR)/%.h: %.in $(CONFIG)
    cp $< $@
    touch $@

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 2014-07-08
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    相关资源
    最近更新 更多