How do I force CMake to use the default compiler on the PATH?

There is the same question and answer. The problem is that the answer seems to be wrong (it is not actually the answer to the question asked). May I ask a question again? 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
...
...

      

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?

0


source to share


2 answers


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

.

+2


source


CMake respects environment variables CC

and CXX

. You can export or just install them for cmake.

CC=gcc CXX=g++ cmake .....

      



Please note that this only works in empty build directories. Changing the compiler in existing directories doesn't work like this.

+1


source







All Articles