Current working directory of makefile

Shashwat picture Shashwat · Nov 14, 2013 · Viewed 24.3k times · Source

I have the following directory structure

project
|-- Makefile
|-- data
    |-- report.tex
    |-- plot_1.eps

One of the rules in the Makefile executes latex data/report.tex. As the working directory is project, I'm getting report.dvi and other output files there only. How do I get those in ./data?

Also, I've included plot_1.eps in the report. But still its expecting the path data/plot_1.eps. Do we need to give the file path relative to the current working directory from where latex is executed? Or location of report.tex?

In the Makefile, I tried

reportdvi: outputparser
        cd data
        latex report.tex
        cd ..

But this didn't change the working directory and the problem persists. What to do?

Answer

ArtemB picture ArtemB · Nov 14, 2013

It's a common 'gotcha' in makefiles. Each command is executed in its own shell, so "cd" only happens within that shell, but subsequent command is run again from make's current directory.

What you want to do is to either put all commands on one line (and you don't need "cd .."):

cd data && latex report.tex

or use \ at the end of the line to tell make to concatenate the lines and pass them all to the shell. Note that you still need ; or && to separate commands.

cd data && \
latex report.tex