90

In order to run make file, I am supposed to go to the make file's directory and from there only I can run the make file. How can I do the same even if i am in any directory?

Dinesh
  • 1,079

5 Answers5

103

General solution

(cd /other/dir && make)

will not change your shell's current directory (because the brackets contents are run in a subshell), but will run make with the indicated working directory.

The && will ensure that make doesn't run if there's an error in the cd part of the command (e.g., the directory doesn't exist, or you don't have access to it).

The above approach is useful for all sorts of commands, not just make. For this reason it is worth learning and remembering.

Specific solution

Check the man page for a -C option, e.g.

make -C /other/dir

Several Unix commands started out without this option but had it added to them at some time (e.g. with GNU implementation)

63

You could use

make -C dir

to change to directory dir before reading the pointed makefile. Please notice that option -C is capitalized.

zyy
  • 219
Jonathan
  • 731
3

This is an old question but I like to use a ~/.bashrc alias, which looks like this:

alias makeaway="cd /dir/to/make && make && cd -"
JW0914
  • 9,096
2

Use cd ./dir && make && pwd inside Makefile .

Example of sample Makefile :

BUILD_DIR       =       $(shell pwd)

deploy::
        cd ./dist/local && make && pwd && npm publish && cd .. && cd .. && pwd
clean::
        npm cache clean --force
1

You can also use it with Makefile targets.

make {your target} -C {your Makefile directory}

For instance:

make flcean -C /home/yourlogin/yourdirectory
MM Temel
  • 81
  • 1
  • 1