cloudrain21
cloudrain21

Reputation: 659

How can I give the filename of current buffer automatically when I execute 'compile' command in Emacs?

As you already know, we can see 'make -k' command when we execute 'M-x compile' in emacs c++ mode.

But, now I'm studying some boost libraries, so I must frequently compile just single file after writing a example code. So, for convenience, I've been doing this like below.

(1) make an alias in shell init file for g++ compile command.
    alias c="g++ -g -Wall -O0 -std=c++11"
(2) map 'M-x compile' command to Function Key F9 in .emacs
    (global-set-key [f9] 'compile)
(3) write c++ code
(4) press F9 and modify filename to current open file

For example, you can see 'Compile command: c vvv.cpp' in the below screen shot. (This screen shot is just a simple code that I made for this question. 'c' is alias for compile command and options) But, this open filename is actually 'vector1.cpp', so 'vvv.cpp' should be automatically replaced by 'vector1.cpp'.

enter image description here

I think this is not bad. But, I'd like to find a way that I don't have to modify the current filename. "Just press F9 and RET, then the current file will be compiled." This is exactly what I want.

Is there any better idea or way I can do like this? Or plz let me know your nice compiling way for a minimal key pressing. Thanks.

Upvotes: 2

Views: 336

Answers (1)

tonio
tonio

Reputation: 10541

The EmacsWiki has a nice section explaining how you can customize the compile command: Compile Command

In your case, you could add a hook to the c++ mode to define the compile-command variable as you need. Following the example on the wiki:

(add-hook 'c++-mode-hook
  (lambda ()
    (set (make-local-variable 'compile-command)
         (format "g++ -g -Wall -O0 -std=c++11 %s" (buffer-name)))))

Adding this snippet to your .emacs should do the trick.

Upvotes: 2

Related Questions