There is the same question and the answer. The problem is that the answer seems to be wrong (actually is not the answer to the asked question). Can I re-ask the question? The problem:
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
$ whereis gcc
cc: /usr/bin/gcc /usr/lib/gcc /usr/local/bin/gcc /usr/libexec/gcc
$ which gcc
/usr/local/bin/gcc
$ /usr/bin/gcc -v
gcc version 4.1.2
$ /usr/local/bin/gcc -v
gcc version 4.8.4
$ gcc -v
gcc version 4.8.4
$ cmake .
-- The C compiler identification is GNU 4.1.2
...
...
The local GCC version if 4.8.4 and the system's default version is '4.1.2'. All other tool-chains respects the PATH environment variable and use the local (newer) GCC version. All except of CMAKE.
Setting CC is not a good idea, because there might be other binary tools which could also be used.
Setting CMAKE_PROGRAM_PATH and CMAKE_PREFIX_PATH in the beginning of the script doesn't help with detection of the compilers.
Is there any way to force CMAKE to respect the PATH variable?
CMake respects the environment variables
CC
andCXX
. You can export or just set them for cmake.Note that this only works in empty build directories. Changing the compiler on existing directories does not work like this.
As is already written in the answer to the other question, CMake prefers the generic compiler names
cc
andc++
when searching for the C and C++ compilers. These probably refer to GNU version 4.1 compilers on your system.Anyway, to force CMake to use the default compilers on the system path, add the following code to the beginning of your outermost
CMakeLists.txt
.The
find_program
calls must occur before the call toproject
orenable_language
.