Emacs でソースコードが複数のディレクトリに分かれている場合にコンパイルする話。エラーが起きたファイルが開けないことありませんか? なんと Entering directory
と書くと emacs が読み取ってくれるんです。
問題の設定
ディレクトリ構造:
Makefile
sub/Makefile
sub/hello.cpp
Makefile: sub
に入って make
する
default:
cd sub && $(MAKE)
sub/Makefile: sub/
の中でコンパイルする
hello: hello.o
$(CXX) $< -o $@
さて、親ディレクトリのファイルを開いている emacs で M+x compile
してエラーがあった場合、C-x ` でエラーの場所に飛んでほしいわけだけど、
Find this error in (default hello.cpp):
と言われる。emacs さんは hello.cpp
が sub/
にあることを知らない。
解決法
なんと "Entering directory sub" というメッセージが出力されると emacs はそれを解する。
Makefile
default:
$(MAKE) --print-directory -C sub
default:
@echo "Entering directory sub"
cd sub && $(MAKE)
@echo "Leaving directory sub"
でもいい
バージョンによってはデフォルトで Entering directory が表示される場合もあるようだ。
これで簡単にエラーの場所に飛べる。オープンソースコードをコンパイルするとよく見るこのメッセージには実はこんな効能が。幸せ。
リンク
How to adjust the path that Emacs compile-goto-error gets from the compilation buffer?
https://stackoverflow.com/questions/10451242/how-to-adjust-the-path-that-emacs-compile-goto-error-gets-from-the-compilation