LoginSignup
3

More than 3 years have passed since last update.

emacs + recursive make "Entering directory" で幸せになる

Last updated at Posted at 2019-07-07

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.cppsub/ にあることを知らない。

解決法

なんと "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

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3