Search code examples
gcccmakeccache

CMake wants to use ccache instead of gcc


I am trying to use CMake to compile a C++ project on Ubuntu, but I get this error:

CMake Error: your C compiler: "/usr/lib64/ccache/bin/gcc" was not found.   Please set CMAKE_C_COMPILER to a valid compiler path or name.
CMake Error: your CXX compiler: "/usr/lib64/ccache/bin/c++" was not found.   Please set CMAKE_CXX_COMPILER to a valid compiler path or name.

I have gcc and everything installed (build-essential on Ubuntu), but I cannot stop cmake from doing this, except by hard-coding CMAKE_C_COMPILER and CMAKE_CXX_COMPILER in CMakeLists.txt, which I am loathe to do for obvious reasons. I tried installing ccache, but that had no effect.


Solution

  • You are not supposed to change CMAKE_C_COMPILER and CMAKE_CXX_COMPILER from within the CMakeLists. Instead you should give them at the command line when running CMake for the first time:

    cmake -D CMAKE_CXX_COMPILER=/path/to/g++ ..
    

    Note that this value has no effect after the first run of CMake! So if you want to switch to a different compiler you will either have to delete the CMake cache or switch to a new build directory.

    On the other hand, not having to specify this lengthy option for subsequent CMake runs of course also saves a lot of typing.