我如何从其他目录调用make文件

How can i call make file from other directory

我有这样的目录结构

containers/con1
containers/con2
containers/con3

现在像 con1、con2 这样的每个文件夹都有 Makefile,其中包含 build, run

这样的目标

我运行喜欢make runmake build

但我必须进入那个文件夹。

有没有可能我在containers/Makefile

中还有另一个Makefile

我可以运行喜欢

Make con1.run Make con2.run

据我了解你想要这个:

   -C dir, --directory=dir
        Change to directory dir before reading the makefiles or doing anything else.  If multiple -C options are specified, each is interpreted relative to the previous one: -C / -C etc is equivalent to -C /etc.   This  is  typi‐
        cally used with recursive invocations of make.

像这样添加 -C 选项:make -C con1/

是的,你可以做到。像下面这样的东西应该做你想做的。

$ cat containers/Makefile
%.run: %
        $(MAKE) -C $@

话虽这么说,正如您所看到的那样,执行您想要的操作的命令非常简单,以至于实际上不需要这样的 makefile(简单的 shell 脚本在这里与 makefile 一样有用)。

$ cat run.sh
[ -d "" ] || { echo 'No such directory.' >&2; exit 1; }
#make -C ""
# OR
#cd "" && make

如果您希望能够一次构建 所有 子目录项目,那么 makefile 可以帮助您,但即便如此也足够简单 shell 一行.

$ for mkfile in */Makefile; do make -C "$(dirname "$mkfile"); done
$ for mkfile in */Makefile; do (cd "$(dirname "$mkfile") && make); done

递归 makes 是邪恶的,但如果你想要那样:

# con1.run is a phony target...
.PHONY: con1.run

con1.run:
    $(MAKE) -C con1