28

I would like to run a makefile from another place in the file system. How do I pass the location of the makefile to make?

if I stand in "/" and I would like to run a makefile that resists in "/dir/dir2/dir3/makefile", how do I add that to the make command?

I tried:

make --file=dir/dir2/dir3/makefile

but it did not worked.

trejder
  • 17,148
  • 27
  • 124
  • 216
rablentain
  • 6,641
  • 13
  • 50
  • 91
  • Possible duplicate of [In Unix, can I run 'make' in a directory without cd'ing to that directory first?](https://stackoverflow.com/questions/453447/in-unix-can-i-run-make-in-a-directory-without-cding-to-that-directory-first) – Flynsee Sep 15 '17 at 02:43
  • ok, so `-C` is not guaranteed to work? I can't make it work with `/lfs/ampere4/0/brando9/proverbot9001/CompCert/configure x86_64-linux && make clean -C /lfs/ampere4/0/brando9/proverbot9001/CompCert` only `./configure x86_64-linux && make` seems to work. – Charlie Parker Dec 14 '22 at 07:34
  • does `make -C` do it? – Charlie Parker Feb 22 '23 at 23:05

2 Answers2

31

All relative paths in the makefile will be relative to your current directory and not the directory of the makefile.

Assuming that you understand that and what you want to do is still going to work then you want the -f flag to specify the makefile to use. (Which is in the man page, the manual and the --help output.)

If, instead, what you mean is you want to cd to somewhere else and run make then perhaps you are looking for (cd /some/path && make)?

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • Maybe the cd suggestion fits best. I guess that, the difference is whether I want the compiled files to be in the same folder as the make file (which would be the cd example), or if I want the files to be in the same dir where I stand. Am I right? – rablentain Jan 20 '15 at 20:29
  • More than that. If you have a rule in the makefile that says `foo: foo.c` that will create a file `foo` in the current directory but will also look for `foo.c` in the current directory and it isn't going to be there. – Etan Reisner Jan 20 '15 at 20:31
  • I have been messing around with my Makefile trying to get `make` to recognize `CFLAGS` arguments I specified therein. But it didn't seem those args were getting passed to `make`. So I removed my local `Makefile` and ran `make` again, and it continued to run the same command (`cc -o ex4.c -o ex4`). I was trying to pass `-g -Wall` via `CFLAGS`. Is there some master `Makefile` hanging around my OS?! – mecampbellsoup Jun 03 '15 at 19:16
  • No. Built-in rules. See [Catalogue of Built-In Rules](http://www.gnu.org/software/make/manual/make.html#Catalogue-of-Rules). – Etan Reisner Jun 03 '15 at 19:35
  • ok, so `-C` is not guaranteed to work? I can't make it work with `/lfs/ampere4/0/brando9/proverbot9001/CompCert/configure x86_64-linux && make clean -C /lfs/ampere4/0/brando9/proverbot9001/CompCert` only `./configure x86_64-linux && make` seems to work. – Charlie Parker Dec 14 '22 at 07:33
  • does `make -C` do it? – Charlie Parker Feb 22 '23 at 23:05
26

You can use the -C flag to specify the path to your makefile. This way you can execute it from a different directory. The -f flag has a different use. With that flag you can execute a makefile with a name other than makefile.

Andries
  • 361
  • 3
  • 5
  • This did the trick for me, I generated my Makefile with cmake and then wanted to build from another directory – Jorge Barroso Feb 28 '20 at 21:48
  • ok, so `-C` is not guaranteed to work? I can't make it work with `/lfs/ampere4/0/brando9/proverbot9001/CompCert/configure x86_64-linux && make clean -C /lfs/ampere4/0/brando9/proverbot9001/CompCert` only `./configure x86_64-linux && make` seems to work. – Charlie Parker Dec 14 '22 at 07:34
  • does `make -C` do it? – Charlie Parker Feb 22 '23 at 23:05