Invoking root directorie’s makefile from subdirectory in EMACS

To compile, M-x compile is used in emacs. The value displayed in the mini-buffer for this command is stored in the compile-command variable. When you execute, make -k, emacs executes the command in the directory on which the currently opened file belongs. This won’t help as you may not have makefile in the current directory.

Most of the projects will have only one makefile which will be available in the root directory. To invoke this makefile from a subdirectory, you can use -f switch and specify the makefile path. Something like:

make -f project_root_directory/Makefile

Still, emacs executes this command from the current directory. This method has got potential problems like relative paths used inside the makefile won’t work. A reliable method would be to ask emacs to change the current directory to project’s root, execute the makefile and come back to the current directory. Fortunately, emacs supports cd command which can be used to change the directory and cd – to come back to last directory. After issuing the compile command (M-x compile), you can type

(cd /project_root_directory && make && cd -)

Using this method, emacs executes the makefile from the root directory. You can add the following to .emacs file if you need this trick always.

(setq compile-command "(cd /project_root_directory && make && cd -)")

Happy programming!

Advertisements