在切换到某个目录之前,Make 会在该目录中寻找依赖项吗?

发布于 2024-10-30 05:12:12 字数 854 浏览 2 评论 0原文

我尝试编写以下形式的 make 规则:

lib%.so: computations/%.h
        make -C computations/ -f makefile $<

输出:

make -C computations/ -f makefile computations/test.h
make[1]: Entering directory `/home/shai/csm/csm2/src/computations'
make[1]: *** No rule to make target `computations/test.h'.  Stop.
make[1]: Leaving directory `/home/shai/csm/csm2/src/computations'
make: *** [libtest.so] Error 2

嗯,看起来它在切换库后查找 %.h 。没什么大不了的,我只是尝试删除

lib%.so: %.h
        make -C computations/ -f makefile $<

,但现在它根本不识别规则!

输出:

shai@ubuntu:~/csm/csm2/src$ make libtest.so
`make: *** No rule to make target `libtest.so'.  Stop.

make 是在玩弄我的想法吗? 依赖关系的变化如何使其停止识别目标

I tried to write a make rule of this form:

lib%.so: computations/%.h
        make -C computations/ -f makefile 
lt;

Output:

make -C computations/ -f makefile computations/test.h
make[1]: Entering directory `/home/shai/csm/csm2/src/computations'
make[1]: *** No rule to make target `computations/test.h'.  Stop.
make[1]: Leaving directory `/home/shai/csm/csm2/src/computations'
make: *** [libtest.so] Error 2

Well, it seems that it looks for %.h after switching library. No biggie, I'll just try removing

lib%.so: %.h
        make -C computations/ -f makefile 
lt;

but now it doesn't recognize the rule at all!

Output:

shai@ubuntu:~/csm/csm2/src$ make libtest.so
`make: *** No rule to make target `libtest.so'.  Stop.

Is make playing with my mind? How could a change in the dependencies make it stop recognizing the target?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赠佳期 2024-11-06 05:12:13

依赖关系的更改如何使其停止识别目标?

很可能是因为它找不到符合依赖关系模式的文件,因为 test.h 位于另一个目录中。您可以使用 VPATH 让它搜索其他目录中的文件。

编辑:但这仍然不能完全解决您的问题,因为 $< 将被 computations/test.h 替换,在目录中找不到它计算(如您遇到的第一个错误)。您可以尝试 $(notdir $<),但我认为这是一个丑陋的黑客(如果它有效;我还没有尝试过)。这是由 makefile 和项目结构的设计造成的。

How could a change in the dependencies make it stop recognizing the target?

Most likely because it can't find a file that fits the pattern of the dependency, because test.h is in another directory. You could use VPATH to have it search for files in other directories.

Edit: but this still won't fix your problem completely, because $< will be substituted by computations/test.h, which won't be found in the directory computations (as in the first error you got). You might try $(notdir $<), but I think it's an ugly hack (if it works; I haven't tried it). This results from the design of your makefile and project structure.

洒一地阳光 2024-11-06 05:12:13

你使用的是错误的。

Make 的设计实际上相当简单:你告诉它

  1. 如何创建目标(规则的主体)。
  2. 它需要首先创建哪个目标(依赖项列表)。

然后对依赖信息进行拓扑排序,以便以正确的顺序执行主体。

您使用 $< 的规则(扩展为依赖项列表)意味着您告诉调用的 make 创建依赖项 %.h 而不是目标 lib%.so
规则必须创建目标。如果需要创建依赖项,您需要告诉 make 如何使用另一个单独的规则来创建依赖项,而不是尝试确保它在此规则中是最新的。

当您尝试跨目录进行协调时,我会推荐论文 Recursive make被认为是有害的,这通常是一种组织事物的好方法,尽管不是唯一可能的做事方式。

You're using make wrong.

Make's design is actually fairly simple at heart: you tell it

  1. how to create targets (The body of the rule).
  2. which targets it needs to create first (The dependency list).

Make then topological sorts the dependency information so it executes the bodies in the right order.

Your rule with $< (which expands to the list of dependencies) means you're telling the invoked make to create the dependency %.h not the target lib%.so.
The rule has to create the target. If the dependency needs to be created, you need to tell make how to do so with another, separate rule, not try to ensure it is up to date in this rule.

As you're trying to coordinate across directories, I'll recommend the paper Recursive make considered harmful, which can often be a nice way to organize things, though not the only possible way to do things.

海夕 2024-11-06 05:12:13

这可能只是您复制/粘贴它的方式,但您在第二种形式中缺少一个选项卡。

It might just be the way you copy/pasted it, but you're missing a tab in the second form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文