如何在makefile中写入"cd"命令? [英] How do I write the 'cd' command in a makefile?

查看:823
本文介绍了如何在makefile中写入"cd"命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我的makefile中有类似的内容:

For example, I have something like this in my makefile:

all:
     cd some_directory

但是,当我键入make时,只看到了'cd some_directory',就像在echo命令中一样.

But when I typed make I saw only 'cd some_directory', like in the echo command.

推荐答案

它实际上是在执行命令,将目录更改为some_directory,但是,这是在子进程shell中执行的,不会影响make或您正在使用的外壳程序.

It is actually executing the command, changing the directory to some_directory, however, this is performed in a sub-process shell, and affects neither make nor the shell you're working from.

如果要在some_directory中执行更多任务,则需要添加分号并附加其他命令.请注意,您不能使用换行符,因为它们被make解释为规则的结尾,因此,为清楚起见而使用的任何换行符都必须使用反斜杠进行转义.

If you're looking to perform more tasks within some_directory, you need to add a semi-colon and append the other commands as well. Note that you cannot use newlines as they are interpreted by make as the end of the rule, so any newlines you use for clarity needs to be escaped by a backslash.

例如:

all:
        cd some_dir; echo "I'm in some_dir"; \
          gcc -Wall -o myTest myTest.c

还请注意,即使您添加了反斜杠和换行符,每个命令之间也必须使用分号.这是由于shell将整个字符串解析为一行的事实.如评论中所述,您应该使用&&"加入命令,这意味着它们仅在前面的命令成功执行后才会执行.

Note also that the semicolon is necessary between every command even though you add a backslash and a newline. This is due to the fact that the entire string is parsed as a single line by the shell. As noted in the comments, you should use '&&' to join commands, which mean they only get executed if the preceding command was successful.

all:
        cd some_dir && echo "I'm in some_dir" && \
          gcc -Wall -o myTest myTest.c

这在进行破坏性工作(例如清理)时尤其重要,因为如果cd由于任何原因而失败,您将销毁错误的东西.

This is especially crucial when doing destructive work, such as clean-up, as you'll otherwise destroy the wrong stuff, should the cd fail for whatever reason.

不过,通常的用法是在您可能要查看的子目录中调用make.有一个命令行选项,因此您不必自己调用cd,因此您的规则将如下所示:

A common usage though is to call make in the sub directory, which you might want to look into. There's a command line option for this so you don't have to call cd yourself, so your rule would look like this

all:
        $(MAKE) -C some_dir all

,它将变为some_dir,并在其中以目标"all"执行Makefile.最佳做法是使用$(MAKE)而不是直接调用make,因为它将注意调用正确的make实例(例如,如果您为构建环境使用特殊的make版本),以及使用某些开关(例如-t)运行时,行为会稍有不同.

which will change into some_dir and execute the Makefile in there with the target "all". As a best practice, use $(MAKE) instead of calling make directly, as it'll take care to call the right make instance (if you, for example, use a special make version for your build environment), as well as provide slightly different behavior when running using certain switches, such as -t.

为记录起见,使 always 总是回显它执行的命令(除非明确禁止显示),即使它没有输出,这也是您所看到的.

For the record, make always echos the command it executes (unless explicitly suppressed), even if it has no output, which is what you're seeing.

这篇关于如何在makefile中写入"cd"命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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