AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 77265659
Accepted
Alex028502
Alex028502
Asked: 2023-10-10 20:10:39 +0800 CST2023-10-10 20:10:39 +0800 CST 2023-10-10 20:10:39 +0800 CST

仅在必要时重建,即使我们依赖于具有自己的 Makefile 的子项目

  • 772

情况

我有一个 Makefile,它依赖于具有自己的 Makefile 的子项目中的一些内容。在子项目中,目标具有我希望我的主项目不必知道的依赖关系。因此,我尝试使用子项目的 Makefile 在必要时从中构建我需要的任何内容。这是我在构建之前在示例项目中的内容:

$ find . -type f | xargs head
==> ./subproject/Makefile <==
binary: source.txt
        echo subproject source: > $@
        date >> $@
        cat $< >> $@
clean:
        rm binary

==> ./subproject/source.txt <==
this is the subproject source code

==> ./Makefile <==
binary: source.txt subproject/binary
        echo main source: > $@
        date >> $@
        cat $^ >> $@
subproject/%:
        $(MAKE) -C $(@D) $(@F)
clean:
        rm binary
        $(MAKE) -C subproject clean


==> ./source.txt <==
this is the main source code

这就是我构建后得到的:

$ make
make -C subproject binary
make[1]: Entering directory '~/make-example/subproject'
echo subproject source: > binary
date >> binary
cat source.txt >> binary
make[1]: Leaving directory '~/make-example/subproject'
echo main source: > binary
date >> binary
cat source.txt subproject/binary >> binary
$ find . -type f | xargs head
==> ./subproject/Makefile <==
binary: source.txt
        echo subproject source: > $@
        date >> $@
        cat $< >> $@
clean:
        rm binary

==> ./subproject/binary <==
subproject source:
Di 10. Okt 14:01:03 CEST 2023
this is the subproject source code

==> ./subproject/source.txt <==
this is the subproject source code

==> ./Makefile <==
binary: source.txt subproject/binary
        echo main source: > $@
        date >> $@
        cat $^ >> $@
subproject/%:
        $(MAKE) -C $(@D) $(@F)
clean:
        rm binary
        $(MAKE) -C subproject clean


==> ./binary <==
main source:
Di 10. Okt 14:01:03 CEST 2023
this is the main source code
subproject source:
Di 10. Okt 14:01:03 CEST 2023
this is the subproject source code

==> ./source.txt <==
this is the main source code
alex@elephant:~/make-example$ 

问题

当我更改subproject/source.txt并make在父项目中运行时,它仍然不会重新编译。

我几乎可以像这样解决这个问题:

subproject/%: always
    $(MAKE) -C $(@D) $(@F)
always:

当make在子项目中运行时,subproject/binary不会被重新编译。但是,binary在父项目中仍然会重新编译,因为它的先决条件之一已运行,即使先决文件上的日期是旧的。


问题

所以我认为答案可能是我不应该这样做。我真的希望父项目和子项目彼此了解得尽可能少。

有没有办法让父项目检查日期subproject/binary并使用它来确定是否重建binary,即使subproject/binary公式已运行?

我希望能够键入make并构建它,然后在主项目中再次更改subproject/source.txt和键入,然后重新编译,然后重新编译结果,然后紧接着,不更改任何内容,然后再次键入,不重新编译任何内容makesubproject/binarybinarymake

makefile
  • 1 1 个回答
  • 15 Views

1 个回答

  • Voted
  1. Best Answer
    MadScientist
    2023-10-10T21:43:38+08:002023-10-10T21:43:38+08:00

    你写:

    然而,父项目中的二进制文件仍然会被重新编译,因为它的先决条件之一已运行,即使先决文件上的日期是旧的。

    make 不是这样工作的。仅仅因为配方被调用并不意味着 make 总是假设目标已被修改;make 将检查先决条件的修改时间并进行比较,即使先决条件的配方被调用也是如此。

    因此,如果您看到binary总是重建,那么这里发生了其他事情:也许子 make 的编写方式总是修改二进制文件。您可能想使用make --trace或make -d了解 make 决定重建的原因binary。

    例子:

    UPDATE = touch $@
    binary: prereq; touch $@
    prereq: FORCE ; : updating; $(UPDATE)
    FORCE:
    

    现在:

    $ make
    : updating; touch prereq
    touch binary
    
    $ make
    : updating; touch prereq
    touch binary
    

    正如预期的那样,但现在如果我们实际上不改变先决条件:

    $ make UPDATE=
    : updating;
    

    我们可以看到先决条件更新规则已运行,但由于它实际上没有改变prereq,所以 make 没有重建binary。

    • 2

相关问题

  • Makefile 压缩目录中的所有文件

  • Makefile 条件中的 $(OS) 是什么?

  • 为什么总是调用“make install”?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    使用 <font color="#xxx"> 突出显示 html 中的代码

    • 2 个回答
  • Marko Smith

    为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类?

    • 1 个回答
  • Marko Smith

    您可以使用花括号初始化列表作为(默认)模板参数吗?

    • 2 个回答
  • Marko Smith

    为什么列表推导式在内部创建一个函数?

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 个回答
  • Marko Smith

    为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)?

    • 4 个回答
  • Marko Smith

    为什么库中不调用全局变量的构造函数?

    • 1 个回答
  • Marko Smith

    std::common_reference_with 在元组上的行为不一致。哪个是对的?

    • 1 个回答
  • Marko Smith

    C++17 中 std::byte 只能按位运算?

    • 1 个回答
  • Martin Hope
    fbrereto 为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 您可以使用花括号初始化列表作为(默认)模板参数吗? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi 为什么列表推导式在内部创建一个函数? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A fmt 格式 %H:%M:%S 不带小数 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python C++20 的 std::views::filter 未正确过滤视图 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute 为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa 为什么库中不调用全局变量的构造函数? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis std::common_reference_with 在元组上的行为不一致。哪个是对的? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev 为什么编译器在这里错过矢量化? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan C++17 中 std::byte 只能按位运算? 2023-08-17 17:13:58 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve