How do I force CMake to use the default compiler on the PATH?
$ 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
...
...
Local version of GCC if 4.8.4 and system default is "4.1.2". All other toolchains respect the PATH environment variable and use the local (newer) version of GCC. Everyone except CMAKE. Setting up CC is not a good idea because there may be other binary tools that can be used as well. Setting CMAKE_PROGRAM_PATH and CMAKE_PREFIX_PATH at the beginning of the script does not help with compiler detection.
Is there a way to make CMAKE respect the PATH variable?
source to share
As stated in the answer to another question , CMake prefers generic compiler names cc
and c++
when looking for C and C ++ compilers.They probably refer to the version 4.1 GNU compilers on your system.
Anyway, to force CMake to use the default compilers on the system path, add the following code to the top of your outer CMakeLists.txt
.
find_program(CMAKE_C_COMPILER NAMES $ENV{CC} gcc PATHS ENV PATH NO_DEFAULT_PATH)
find_program(CMAKE_CXX_COMPILER NAMES $ENV{CXX} g++ PATHS ENV PATH NO_DEFAULT_PATH)
...
project (Foo C CXX)
Calls find_program
must be made before calling project
or enable_language
.
source to share