How to use ccache with Make?

peeyush picture peeyush · Mar 18, 2012 · Viewed 11.9k times · Source

I have a source directory which uses makefile to compile the code. This makefile/configure file is not written for ccache compatibility. So I thought to use ccache. I created alias in .bashrc as alias gcc='ccache gcc', but Makefile is still not considering this definition of gcc. So is there anything I can do without touching Makefile/configure file such that it takes ccache gcc instead of gcc. Also CC='ccache gcc' ./configure is not an option, since it does not ask for CC.

If I write Makefile then I can provide ${gcc), but this is not an option, since I am not writing Makefile. Is there any way by which we don't need to change anything in source file, but still enable ccache compiling.

Answer

MadScientist picture MadScientist · Mar 18, 2012

Aliases are local to the shell they are created in; unlike environment variables they are not passed to any programs that the shell invokes (including make). Make invokes /bin/sh, not /bin/bash, and /bin/sh doesn't read your ~/.bashrc etc. so no aliases defined there will be helpful to you.

I'm not exactly sure why you have placed some of the restrictions you mention on yourself: these things work fine and you haven't given a reason to avoid them that I understand. For example, providing a different CC with configure will work, if the version of autoconf is not really ancient. You can do this:

./configure CC='ccache gcc'

for example and that will set the default value of CC in your makefile to be ccache gcc. I don't know what you mean by "it does not ask for CC".

If you would prefer, you can also override the setting of CC on the make command line, like this:

make CC='ccache gcc'

which also works fine.