The Computer Oracle

How to run make file from any directory?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: RPG Blues Looping

--

Chapters
00:00 How To Run Make File From Any Directory?
00:19 Accepted Answer Score 97
01:04 Answer 2 Score 59
01:19 Answer 3 Score 1
01:37 Answer 4 Score 2
01:48 Thank you

--

Full question
https://superuser.com/questions/370575/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#make #makefile

#avk47



ACCEPTED ANSWER

Score 97


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)




ANSWER 2

Score 59


You could use

make -C dir

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




ANSWER 3

Score 2


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



ANSWER 4

Score 1


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