In Unix, can I run 'make' in a directory without cd'ing to that directory first?

LinuxUnixMakefile

Linux Problem Overview


In Unix, can I run make in a directory without cd'ing to that directory first?

Linux Solutions


Solution 1 - Linux

make -C /path/to/dir

Solution 2 - Linux

As noted in other answers, make(1) has a -C option for this; several commands have similar options (e.g. tar). It is useful to note that for other commands which lack such options the following can be used:

(cd /dir/path && command-to-run)

This runs the command in a sub-shell which first has its working directory changed (while leaving the working directory of the parent shell alone). Here && is used instead of ; to catch error cases where the directory can not be changed.

Solution 3 - Linux

If the reason you don't want to cd to a directory is because you need to stay in the current directory for a later task, you can use pushd and popd:

pushd ProjectDir ; make ; popd

That goes into the ProjectDir, runs make, and goes back to where you were.

Solution 4 - Linux

Also you may use:

make --directory /path/to/dir

Solution 5 - Linux

makefile:

all:
    gcc -Wall -Wpedantic -std=gnu99 -g src/test.c -o build/test

run:
    ./build/test

or

run:
    ./../build/test

etc.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser46795View Question on Stackoverflow
Solution 1 - LinuxbmotmansView Answer on Stackoverflow
Solution 2 - LinuxDave CView Answer on Stackoverflow
Solution 3 - LinuxEnigmaCurryView Answer on Stackoverflow
Solution 4 - Linuxkorst1kView Answer on Stackoverflow
Solution 5 - LinuxoldpilsburyView Answer on Stackoverflow