Say my directory structure is this:
foo
foo/Makefile
foo/bar
Now say foo/Makefile
has a make target baz
.
I want to call make baz
from foo/bar
without creating another Makefile in the bar
subdirectory. Is this possible?
Say my directory structure is this:
foo
foo/Makefile
foo/bar
Now say foo/Makefile
has a make target baz
.
I want to call make baz
from foo/bar
without creating another Makefile in the bar
subdirectory. Is this possible?
GNU make
has two important options for your situation: -f FILE
is used to tell make
which makefile(s) to use instead of the defaults and -C DIR
tells make
to change to directory DIR
before reading the makefiles. How to use one or the other or both depends on your specific case. Note that -f
is compliant with the POSIX standard while -C
is an extension supported by GNU make
. If -C
is not supported by your own version of make
and has no equivalent you will have to change the current directory yourself before invoking make
, e.g. ( cd some/where; make...; )
.
If you can build baz
from foo/bar/
, as suggested by Oo.oO, you can simply
make -f ../Makefile baz
make
will run from foo/bar/
and build baz
as indicated in ../Makefile
.
If you must be in foo/
to build baz
you should use:
make -C .. baz
make
will change to ..
, that is, foo
before reading the makefiles and as it will find here one of the defaults (Makefile
) it will use it to discover how to build baz
.
If you must be in another directory, e.g. the parent of foo/
, you need both options and type:
make -C ../.. -f foo/Makefile baz
make
will first change to ../..
(parent directory of foo/
) and from here it will use foo/Makefile
to discover how to build baz
.