【问题标题】:Is dependency on a symlink possible in a Makefile?Makefile 中是否可能依赖符号链接?
【发布时间】:2012-04-16 20:07:40
【问题描述】:

我的项目中需要几个符号链接。

src/openlayers,文件夹imgtheme 必须在contrib/openlayers 中进行符号链接。 contrib/openlayers 文件夹也应该自动创建。

.PHONY: run
run: contrib/openlayers/theme contrib/openlayers/img
   ../bin/pserve development.ini --reload

contrib/openlayers/theme:
    ln -s src/openlayers/theme $@

contrib/openlayers/img:
    ln -s src/openlayers/img $@

但是这个规则每次都会尝试创建符号链接。 (我将-f 标记为ln,因此它每次都重新创建符号链接。)

【问题讨论】:

    标签: makefile gnu-make symlink


    【解决方案1】:

    如果您遇到这个问题,尽管您的符号链接正确指向现有文件:还请记住,“make”查看符号链接目标文件的 mtime,而 不是 在符号链接本身的时间。

    因此,如果调用“ln -s”的规则具有比符号链接指向的文件的任何依赖项,则“make”必须重新运行该规则中的每个命令时间。它将一次又一次地这样做,因为创建指向文件的符号链接不会更新该文件的 mtime。

    您可以使用“touch”命令来确保链接的目标位置的 mtime 比您的依赖项更新。

    【讨论】:

    【解决方案2】:

    当然,这可以工作。 Make 将所有内容都视为文件,包括符号链接。它将检查文件是否存在(由于您没有列出任何先决条件,因此没有时间戳比较)。在符号链接的情况下,它实际上是在检查链接指向的任何内容,当然,不是链接本身。

    您没有显示执行此操作时会发生什么,但根据您的描述,发生了两件事之一:(a) contrib/openlayers 目录不存在,因此 ln 命令生成错误而不是创建符号链接,所以 make 当然会在下次运行时尝试重新创建它,或者(b)您的符号链接创建不正确并且指向任何内容,这意味着当 make 尝试查看它是否存在时,它会失败并且 make 将尝试重新创建它。

    例如,如果您的src 目录是您的contrib 目录的兄弟,那么您的符号链接就是错误的;你会得到:

    contrib/openlayers/theme -> src/openlayers/theme
    

    或者,当内核尝试解决它时:

    contrib/openlayers/src/openlayers/theme
    

    这不太可能是您想要的。我建议你使用这样的东西:

    contrib/openlayers/theme:
            mkdir -p contrib/openlayers
            ln -s ../../src/openlayers/theme contrib/openlayers/theme
    

    然后验证符号链接在创建后是否确实指向您想要的位置。

    【讨论】:

      猜你喜欢
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      相关资源
      最近更新 更多