My c/c++ projects tend to have fairly straightforward directory structures which separate out src, include, bin, etc. I also tend to have a master makefile in the uppermost directory. When working like this in Emacs, I always have to issue M-x cd uppermost-dir in order for my compilation shortcuts to work as expected.
Is there a way to keep the current directory the same as the one from which I launch Emacs? That is, can I stop Emacs from changing it's working directory when I open a file?
Alternatively, is there something crucial I'm missing about the typical workflow with a directory hierarchiy like this exclusively in Emacs?
One way you can do this:
Make a file in the root directory of your project call .dir-locals.el
This will be read whenever you open a file in the directory or it's sub-directories.
In order to back up to the root folder and run make as your compile command, just put this in the .dir-locals.el
file.
((nil . ((compile-command . "cd ~/mycode/c/; make"))))
nil
is the mode to set local variables for (nil means any), so to do this for only C++ mode you could do this instead ...
((c++-mode . ((compile-command . "cd ~/mycode/c/; make"))))
Obviously you can set up a list with more options, say running ant for java files and so on.
emacs manual entry for directory locals
Invoke make
with the --directory
argument to force it to change to that directory before doing anything:
make --directory /path/to/your/project
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With