Why does CMake refuse to use a non-default compiler?

cmake

Solution

From link you provide:

This method is not guaranteed to work for all generators

Use the second method:

cmake -G "Your Generator" -D CMAKE_C_COMPILER=gcc-4.4 -D CMAKE_CXX_COMPILER=g++-4.4 path/to/your/source

If it not help:

- use absolute path

- do not forget to clean all in build directory

Problem

I want to check whether my C++ project compiles on an older version of GCC. For this reason I have installed the older version and want CMake to use it to compile my project. The CMake FAQ on changing the compiler tells me that this is the right way to do it: ``` CC=gcc-4.4 CXX=g++-4.4 cmake -G "Unix Makefiles" .. ``` So this is what I typed and CMake seems to run fine: ``` chris@chris-machine:~/projects/myProject/build$ CC=gcc-4.4 CXX=g++-4.4 cmake -G "Unix Makefiles" .. -- Found PythonInterp: /usr/bin/python (found version "2.7.4") -- Looking for include file pthread.h -- Looking for include file pthread.h - found -- Looking for pthread_create -- Looking for pthread_create - not found -- Looking for pthread_create in pthreads -- Looking for pthread_create in pthreads - not found -- Looking for pthread_create in pthread -- Looking for pthread_create in pthread - found -- Found Threads: TRUE -- Configuring done -- Generating done -- Build files have been written to: /home/chris/projects/myProject/build ``` However, looking at CMakeCache.txt now I find this: ``` //CXX compiler. CMAKE_CXX_COMPILER:FILEPATH=/usr/bin/c++ ``` Obviously CMake did not use the compiler I specified. When I change this line to use g++-4.4 and run CMake again it will create an endless loop: ``` chris@chris-machine:~/projects/myProject/build$ CC=gcc-4.4 CXX=g++-4.4 cmake -G "Unix Makefiles" .. -- Configuring done You have changed variables that require your cache to be deleted. Configure will be re-run and you may have to reset some variables. The following variables have changed: CMAKE_CXX_COMPILER= /usr/bin/g++-4.4 -- Found PythonInterp: /usr/bin/python (found version "2.7.4") -- Looking for include file pthread.h -- Looking for include file pthread.h - found -- Looking for pthread_create -- Looking for pthread_create - not found -- Looking for pthread_create in pthreads -- Looking for pthread_create in pthreads - not found -- Looking for pthread_create in pthread -- Looking for pthread_create in pthread - found -- Found Threads: TRUE -- Configuring done You have changed variables that require your cache to be deleted. Configure will be re-run and you may have to reset some variables. The following variables have changed: CMAKE_CXX_COMPILER= /usr/bin/g++-4.4 -- Found PythonInterp: /usr/bin/python (found version "2.7.4") // and so on... ``` Why does CMake not use the compiler I specify and how can I fix this?

Original source