1

I asked this question originally on StackOverflow. 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?

3
  • 2
    I'm voting to close this question as off-topic because it's about Make, not Emacs, and better fit for Stack Overflow.
    – user227
    Commented Feb 19, 2015 at 7:55
  • Related: How can I customize the compile command?
    – itsjeyd
    Commented Feb 19, 2015 at 8:05
  • Consider trying out flycheck, which has Clang integration and would provide you with on-the-fly (hence the name) error messages in the buffer without needing to invoke M-x compile.
    – paprika
    Commented Feb 19, 2015 at 13:01

3 Answers 3

2

The question is about how to change the compiler that Make uses, and has nothing to do with Emacs.

If you do not have a Makefile, if your Makefile uses automatic rules, or if it sticks to the standard variable names, you can use make CC=clang CFLAGS='-Wall -Werror -std=c99 -ggdb -O0' MYTARGET as compilation command, where MYTARGET is the name of the target to build (or the program name, if you don't have a Makefile).

If the Makefile uses custom rules, though, you need to take a look at the Makefile itself to find how if and how you can override the compiler. There is no general setting to change compilers and commands used by Make.

2

You can set a variable in make from the command line, like so:

make CC=clang

When you M-x compile you will notice it presents you with the shell command it is going to run make -k -- you can edit this, and if you do it will be presented to you next time you run compile.

So you only need to edit this once per session, which sounds ideal for small student projects.

0
1

I think this is more of a shell question than an Emacs one.

One way to do this is to put a shell script named "cc" early in your $PATH and have it invoke your preferred compiler. Make sure Emacs has this same value of $PATH (not normally a problem).

The underlying problem is that an alias generally won't be invoked by make, whereas a program on $PATH will be.

Not the answer you're looking for? Browse other questions tagged or ask your own question.