如果需要,如何创建目录? [英] How to create directory if needed?

查看:50
本文介绍了如果需要,如何创建目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用makefile,我的问题是我有一个目录,其中包含src目录和Makefile.我还使用了一个tmp目录,该目录我也必须创建. 它具有目标文件.为了提高性能,我尝试在调试时不要删除它们. 如果我创建了一个tmp规则来创建它,它将始终重建所有C文件. 那么该怎么做呢?

I'm trying to use makefile, my problem is that I have a directory, with an src directory and a Makefile. I also use a tmp directory what I also have to create. It has object files in it. And for performance, I try not to delete these when debugging. If I create a rule for tmp to create it, it always rebuilds all the C files. So how to do this?

推荐答案

有很多方法.这取决于您所使用的make版本以及所使用的操作系统.

There are a number of ways. It depends on what version of make you're using and what operating system you're using.

您永远不要做的一件事就是使用目录作为简单的先决条件.文件系统用于更新目录修改时间的规则在make上不能很好地工作.

The one thing you must NEVER do is use a directory as a simple prerequisite. The rules the filesystem uses to update the modified time on directories do not work well with make.

对于POSIX系统上的简单make,您可以在规则中预先创建目录以执行编译:

For simple make on a POSIX system, you can pre-create the directory in the rule to perform the compilation:

obj/foo.o: foo.c
        @ mdkir -p obj
        $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<

如果有GNU,则有两个选择.最简单的方法是通过添加如下行来强制在读取makefile之前先创建目录:

If you have GNU make you have two options. The simplest one is simply to force the directory to be created when the makefile is read in before anything else happens, by adding a line like this:

_dummy := $(shell mkdir -p obj)

简单,快速且可靠.唯一的缺点是即使不需要目录,也将始终创建目录.尽管如此,这是我通常这样做的方式.

The is easy, fast, and reliable. Its only downside is that the directory will be created always, even if it's not needed. Nevertheless this is the way I usually do it.

如果您有足够新的GNU品牌,最花哨的方法是使用仅订购的先决条件:

The most fancy way, if you have a new-enough GNU make, is to use order-only prerequisites:

obj/foo.o: foo.c | obj
        $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<

obj:
        mkdir -p $@

这会强制obj目标在obj/foo.o目标之前构建,但是在确定obj/foo.o是否过时时,会忽略obj上的时间戳.

This forces the obj target to be built before the obj/foo.o target, but the timestamp on obj is ignored when determining whether obj/foo.o is out of date.

这篇关于如果需要,如何创建目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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