【问题标题】:Including another Makefile results in a failed build, why?包含另一个 Makefile 会导致构建失败,为什么?
【发布时间】:2017-04-07 14:31:48
【问题描述】:

问题

包含一个外部 Makefile 使我之前的稳定构建失败并出现以下错误:

make: *** 没有规则来制作目标 'build/main.o','all' 需要。停下来。

上下文

上下文是我目前使用每个项目一个 Makefile 来管理我的每个项目的构建。由于项目的 Makefile 有很多冗余,我想把通用的东西放在一个外部的 Makefile 中,该 Makefile 将包含在每个项目的 Makefile 中。

重现问题的最小示例

在这个简单而简约的示例中,我尝试将 srcs/main.c 构建到 build/main.o 中,并且我还尝试显示变量 FOO 中的内容,其中 tools.mk :

文件夹结构:

|   Makefile
|
+---build
|       (main.o)
+---mkf
|       tools.mk
|
\---srcs
        main.c

Makefile 的内容:

include ./mkf/tools.mk

mkfile_path :=$(realpath $(lastword $(MAKEFILE_LIST)))
current_dir :=$(dir $(mkfile_path))

VPATH = $(current_dir)/srcs
./build/%.o: %.c
    @echo $< $@
    gcc $< -o $@

all: ./build/main.o test
    @echo Done ! 

test:
    @echo Testing include : $(FOO)  

.PHONY: all test

tools.mk 的内容:

FOO = 42

ma​​in.c 的内容(基本的 hello world):

# include <stdio.h>
# include <stdlib.h>

int main(void)
{   
    printf("Hello, world!");
    return EXIT_SUCCESS;
} /*main*/

现在基本上我的问题是,如果我将自己放在根文件夹中并输入make all,构建将失败并出现上述错误。但是,如果我评论 include ./mkf/tools.mk 行,则构建成功。因此我猜它因为包含行而失败,但我不知道为什么。

有人可以启发我吗?

在Windows 7 64-bits 上使用GNU Make 4.2 执行构建。

【问题讨论】:

    标签: makefile build gnu-make


    【解决方案1】:

    在

    mkfile_path :=$(realpath $(lastword $(MAKEFILE_LIST)))
    

    这会产生最后一个包含的 makefile 的路径,即./mkf/tools.mk。详情请见Other Special Variables。

    修复:

    mkfile_path :=$(realpath $(lastword $(MAKEFILE_LIST)))
    current_dir :=$(dir $(mkfile_path))
    include ./mkf/tools.mk
    

    【讨论】:

    • 哇,这很棘手!谢谢您的回答。我会在几天内尝试,但如果它有效,我不会忘记接受你的答案:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多