Linux 在 Unix 中,我可以在目录中运行 'make' 而不先 cd 到该目录吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/453447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:53:46  来源:igfitidea点击:

In Unix, can I run 'make' in a directory without cd'ing to that directory first?

linuxunixmakefile

提问by user46795

In Unix, can I run makein a directory without cd'ing to that directory first?

在 Unix 中,我可以make在不cd先进入该目录的情况下在目录中运行吗?

采纳答案by bmotmans

make -C /path/to/dir

make -C /path/to/dir

回答by Dave C

As noted in other answers, make(1) has a -Coption for this; several commands have similar options (e.g. tar). It is useful to note that for other commands which lack such options the following can be used:

正如其他答案中所述,make(1) 对此有一个-C选项;几个命令有类似的选项(例如 tar)。需要注意的是,对于缺少此类选项的其他命令,可以使用以下命令:

(cd /dir/path && command-to-run)

This runs the command in a sub-shell which first has its working directory changed (while leaving the working directory of the parent shell alone). Here &&is used instead of ;to catch error cases where the directory can not be changed.

这将在子 shell 中运行该命令,该子 shell 首先更改其工作目录(同时保留父 shell 的工作目录)。这里&&用来代替;捕获目录无法更改的错误情况。

回答by EnigmaCurry

If the reason you don't want to cd to a directory is because you need to stay in the current directory for a later task, you can use pushd and popd:

如果你不想 cd 到某个目录的原因是因为你需要留在当前目录中进行以后的任务,你可以使用 pushd 和 popd:

pushd ProjectDir ; make ; popd

That goes into the ProjectDir, runs make, and goes back to where you were.

这会进入 ProjectDir,运行 make,然后返回到您所在的位置。

回答by korst1k

Also you may use:

你也可以使用:

make --directory /path/to/dir

回答by oldpilsbury

makefile:

生成文件:

all:
    gcc -Wall -Wpedantic -std=gnu99 -g src/test.c -o build/test

run:
    ./build/test

or

或者

run:
    ./../build/test

etc.

等等。