在makefile中,是目录名称假目标或“真实”目标? [英] In a makefile, is a directory name a phony target or "real" target?

查看:539
本文介绍了在makefile中,是目录名称假目标或“真实”目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我已经阅读关于makefiles,假目标是任何目标,不对应一个实际的文件名。我的直觉说,作为目标的目录将被视为与文件相同。



为什么这很重要?我有一个目录作为目标在我的makefile。当我有它作为我的主要可执行文件的先决条件,可执行文件总是得到,无论是否一切都是最新的。如果我把它作为先决条件,我的makefile是聪明的,知道什么时候需要构建,但我有一个问题,不知道是否需要创建目录。根据我已经阅读关于make,任何假目标是不好的先决条件,因为make不知道他们是否是最新的,所以他们将永远重建相关的目标。这里是我的makefile的摘录。

  $(EXEC_WITH_PATH):$ {OBJ_DIR} $(DPEND)$(OBJS)
@echo ------------------------------------------;
@echo$(THIS_DIR)$(MACHINE);
@echo链接共享库;
@echoar -rc $(EXEC_WITH_PATH)INSERT :: {OBJS};
ar -rc $(EXEC_WITH_PATH)$(OBJS);
@echo------------------------------------------- - ;


#为目标代码和链接创建目录
$ {OBJ_DIR}:
@if [! -d $ {OBJ_DIR}];然后\
mkdir $ {OBJ_DIR}; \
fi;

在这种情况下, $ {OBJ_DIR} ,目录名,假目标?

解决方案

编辑:这只适用于GNU make - 一个公平的假设给出了linux标签。
$ b

你的目标是一个真正的目标,而不是一个PHONY。问题是,当您将目标放在其中时,输出目录会更新,因此它总是比您的目标更新。这意味着你的目标将始终被构建,因为它的依赖关系已过时。



你需要的是一个 order-only先决条件。这些先决条件允许您指定如果它不存在则需要构建它,但不要注意时间戳。



您可以这样指定:

  $(EXEC_WITH_PATH):$(DPEND)$(OBJS)| $ {OBJ_DIR} 
...

 现在可以使OBJ_DIR目标更简单:

$ {OBJ_DIR}:
mkdir -p $ {OBJ_DIR}

注意:使用 $ {OBJ_DIR} 语法而不是 $(OBJ_DIR),因为这是您使用的。仅限订单的先决条件不依赖于该语法。


According to what I have read about makefiles, a phony target is any target that does not correspond to an actual filename. My intuition says that a directory as a target would be treated the same as a file.

Why is this important? I have a directory as a target in my makefile. when I have it as a prerequisite to my primary executable, that executable always gets made, whether or not everything is up to date. If I take it out as a prerequisite, my makefile is smart enough to know when things need to be built, but I have the problem of not knowing if the directory needs to be created. According to what I have read about make, any phony targets are not good as prerequisites because make does not know if they are up to date, so they will always rebuild the associated target. Here is an excerpt from my makefile.

$(EXEC_WITH_PATH): ${OBJ_DIR} $(DPEND) $(OBJS)
    @echo "--------------------------------------------";
    @echo "$(THIS_DIR)  $(MACHINE)";
    @echo "Linking Shared Library";
    @echo "ar -rc $(EXEC_WITH_PATH) INSERT::{OBJS}";
    ar -rc $(EXEC_WITH_PATH)  $(OBJS);
    @echo "--------------------------------------------";


# Make dirs for object code and links
${OBJ_DIR} :
        @if [ ! -d ${OBJ_DIR} ]; then \
                mkdir ${OBJ_DIR};    \
        fi;

So in this case, is ${OBJ_DIR}, a directory name, a phony target or not?

解决方案

Edit: This only applies to GNU make - a fair assumption given the "linux" tag.

Your target is a real target, not a PHONY one. The problem is that the output directory gets updated when you put targets in it so that it is always newer than your target. This means your target will always get built because it is out of date with respect to its dependencies.

What you need is an order-only prerequisite. These prerequisites allow you to specify that it needs to be built if it does not exist, but not to pay attention to the timestamp. That is, a target will not be out of date with respect to order-only prerequisites.

You specify it like this:

$(EXEC_WITH_PATH): $(DPEND) $(OBJS) | ${OBJ_DIR}
...

Anything after the vertical bar is an order-only prerequisite.

You can now make your OBJ_DIR target simpler:

${OBJ_DIR} :
        mkdir -p ${OBJ_DIR}

Note: I've used the ${OBJ_DIR} syntax instead of $(OBJ_DIR) because that is what you used. Order-only prerequisites do not depend on that syntax.

这篇关于在makefile中,是目录名称假目标或“真实”目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆