haziz
haziz

Reputation: 13602

Change Default Compiler Within Emacs?

I use M-x compile within Emacs to compile my C code which then initiates make -k and allows me to compile the code. I wish to use Clang (or conceivably GCC 4.8 after I install it) as the default compiler. I have cc aliased to clang -Wall -Werror -std=c99 -ggdb -O0 and while this invokes Clang from the command line outside of Emacs, invoking M-x compile from within Emacs still seems to alias cc to GCC version 4.7 which is what I have installed. I wish to tap into the richer and more understandable error and warning messages provided by Clang (and GCC 4.8) but do not wish to create a separate makefile for every short student-level program I am writing, since I am currently going through K&R including solving the exercises.

How do I convince Emacs that M-x compile and make -k should invoke Clang (or GCC 4.8) instead of the older version of GCC?

Upvotes: 5

Views: 1448

Answers (4)

jafrog
jafrog

Reputation: 406

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

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

It will use current file name by default.

Upvotes: 0

Lazylabs
Lazylabs

Reputation: 1444

Assuming that in your makefile, you are using $(CC) to compile your code, you can do one of the following:

  • When you do M-x compile, you can change the compile command to CC=clang make -k.

  • Add the following line in your .emacs file:

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

Upvotes: 1

Dan
Dan

Reputation: 12705

You could write a makefile, and explicitly use clang on the compilation line.

Something like this would work:

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

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

Note that the last line needs to begin with a tab in order to actually work.

Upvotes: 2

teppic
teppic

Reputation: 8205

This isn't emacs, it's make. It defaults to using the environment variable CC, which in turn defaults to gcc. Just run this before you start emacs (assuming you're using Unix):

$ export CC=clang

Alternatively, use a makefile that specifies CC directly.

Upvotes: 9

Related Questions