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

时间:2015-02-13 21:26:06

标签: linux shell ubuntu makefile

我有像这样的目录结构

containers/con1
containers/con2
containers/con3

现在,像con1,con2这样的每个文件夹都包含Makefile,目标类似于build, run

我像make runmake build

一样运行它

但我必须进入该文件夹。

我可能在Makefile

中有另一个containers/Makefile

我可以像

一样运行

Make con1.run Make con2.run

3 个答案:

答案 0 :(得分:1)

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

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

这就是说,你可以看到执行你想要的命令是微不足道的,使得这样的makefile不是真正必要的(而且一个简单的shell脚本在这里和makefile一样有用)。

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

如果您希望能够一次构建所有子目录项目,那么makefile可以帮助您解决这个问题,但即便如此,这也是一个简单的shell单行程序。

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

答案 1 :(得分:0)

据我所知,你想要这个:

   -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/

答案 2 :(得分:0)

递归制作是邪恶的,但如果你想要:

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

con1.run:
    $(MAKE) -C con1