Change default compiler in Emacs?

I use M-x compileEmacs to compile my C code, which then initiates make -kand allows me to compile the code. I want to use Clang (or, presumably, GCC 4.8 after installing it) as the default compiler. I have ccaliased up clang -Wall -Werror -std=c99 -ggdb -O0, and although this calls Clangfrom a command line outside of Emacs, the call M-x compilefrom Emacs still seems to be an alias ccin GCC version 4.7, which I installed. I want to use the richer and clearer error and warning messages provided by Clang (and GCC 4.8), but I don’t want to create a separate make-up file for each short student-level program that I am writing, as I am currently viewing K & R, including solving exercises.

How to convince Emacs that M-x compilethey make -kshould call Clang (or GCC 4.8) instead of the old version of GCC?

+5
source share
4 answers

You can write a makefile and explicitly use clang in the compilation line.

Something like this will work:

CC = clang
CFLAGS = -Wall -Werror -std = c99 -ggdb -O0

%:% .c
	$ (CC) $ (CFLAGS) $ ^ -o $ @

Note that the last line must begin with a bookmark in order to actually work.

+1
source

These are not emacs, do this. The default environment variable is CC, which defaults to gcc. Just run this before starting emacs (if you are using Unix):

$ export CC=clang

Also, use a makefile that directly points to CC.

+8
source

, make $(CC) , :

  • M-x compile, CC=clang make -k.

  • Add the .emacsfollowing line to your file :

    (setq compile-command "CC=clang make -k")

+1
source

You can also do this without make. @Lazylabs mentioned that you can change the value compile-command. To make it mode specific, add it to your Emacs configuration:

(add-hook 'c-mode-hook
      (lambda ()
        (setq compile-command 
              (concat "clang -Wall -Werror -std=c99 -ggdb -O0" buffer-file-name))))

It will use the current default file name.

0
source

All Articles